amdgpu_probe.c revision 46845023
1/* 2 * Copyright 2000 ATI Technologies Inc., Markham, Ontario, and 3 * VA Linux Systems Inc., Fremont, California. 4 * 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining 8 * a copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation on the rights to use, copy, modify, merge, 11 * publish, distribute, sublicense, and/or sell copies of the Software, 12 * and to permit persons to whom the Software is furnished to do so, 13 * subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial 17 * portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 * NON-INFRINGEMENT. IN NO EVENT SHALL ATI, VA LINUX SYSTEMS AND/OR 23 * THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 * DEALINGS IN THE SOFTWARE. 27 */ 28 29#ifdef HAVE_CONFIG_H 30#include "config.h" 31#endif 32 33#include <errno.h> 34#include <string.h> 35#include <stdlib.h> 36#include <sys/stat.h> 37#include <fcntl.h> 38 39/* 40 * Authors: 41 * Kevin E. Martin <martin@xfree86.org> 42 * Rickard E. Faith <faith@valinux.com> 43 * KMS support - Dave Airlie <airlied@redhat.com> 44 */ 45 46#include "amdgpu_probe.h" 47#include "amdgpu_version.h" 48#include "amdgpu_drv.h" 49 50#include "xf86.h" 51 52#include "xf86drmMode.h" 53 54#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 55#include <xf86_OSproc.h> 56#endif 57 58#include <xf86platformBus.h> 59 60_X_EXPORT int gAMDGPUEntityIndex = -1; 61 62/* Return the options for supported chipset 'n'; NULL otherwise */ 63static const OptionInfoRec *AMDGPUAvailableOptions(int chipid, int busid) 64{ 65 return AMDGPUOptionsWeak(); 66} 67 68static SymTabRec AMDGPUAny[] = { 69 { 0, "All GPUs supported by the amdgpu kernel driver" }, 70 { -1, NULL } 71}; 72 73/* Return the string name for supported chipset 'n'; NULL otherwise. */ 74static void AMDGPUIdentify(int flags) 75{ 76 xf86PrintChipsets(AMDGPU_NAME, "Driver for AMD Radeon", AMDGPUAny); 77} 78 79static Bool amdgpu_device_matches(const drmDevicePtr device, 80 const struct pci_device *dev) 81{ 82 return (device->bustype == DRM_BUS_PCI && 83 device->businfo.pci->domain == dev->domain && 84 device->businfo.pci->bus == dev->bus && 85 device->businfo.pci->dev == dev->dev && 86 device->businfo.pci->func == dev->func); 87} 88 89static Bool amdgpu_kernel_mode_enabled(ScrnInfoPtr pScrn) 90{ 91#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 92 AMDGPUEntPtr pAMDGPUEnt = AMDGPUEntPriv(pScrn); 93 const char *busIdString = pAMDGPUEnt->busid; 94 int ret = drmCheckModesettingSupported(busIdString); 95 96 if (ret) { 97 if (xf86LoadKernelModule("amdgpukms")) 98 ret = drmCheckModesettingSupported(busIdString); 99 } 100 if (ret) { 101 xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 0, 102 "[KMS] drm report modesetting isn't supported.\n"); 103 return FALSE; 104 } 105 106#endif 107 xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 0, 108 "[KMS] Kernel modesetting enabled.\n"); 109 return TRUE; 110} 111 112static int amdgpu_kernel_open_fd(ScrnInfoPtr pScrn, 113 struct pci_device *pci_dev, 114 struct xf86_platform_device *platform_dev, 115 AMDGPUEntPtr pAMDGPUEnt) 116{ 117#define MAX_DRM_DEVICES 64 118 drmDevicePtr devices[MAX_DRM_DEVICES]; 119 struct pci_device *dev; 120 const char *path; 121 int fd = -1, i, ret; 122 123 if (platform_dev) 124 dev = platform_dev->pdev; 125 else 126 dev = pci_dev; 127 128 XNFasprintf(&pAMDGPUEnt->busid, "pci:%04x:%02x:%02x.%u", 129 dev->domain, dev->bus, dev->dev, dev->func); 130 131 if (platform_dev) { 132#ifdef ODEV_ATTRIB_FD 133 fd = xf86_get_platform_device_int_attrib(platform_dev, 134 ODEV_ATTRIB_FD, -1); 135 if (fd != -1) 136 return fd; 137#endif 138 139#ifdef ODEV_ATTRIB_PATH 140 path = xf86_get_platform_device_attrib(platform_dev, 141 ODEV_ATTRIB_PATH); 142 143 fd = open(path, O_RDWR | O_CLOEXEC); 144 if (fd != -1) 145 return fd; 146#endif 147 } 148 149 if (!amdgpu_kernel_mode_enabled(pScrn)) 150 return -1; 151 152 ret = drmGetDevices2(0, devices, ARRAY_SIZE(devices)); 153 if (ret == -1) { 154 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, 155 "[drm] Failed to retrieve DRM devices information.\n"); 156 return -1; 157 } 158 for (i = 0; i < ret; i++) { 159 if (amdgpu_device_matches(devices[i], dev) && 160 devices[i]->available_nodes & (1 << DRM_NODE_PRIMARY)) { 161 path = devices[i]->nodes[DRM_NODE_PRIMARY]; 162 fd = open(path, O_RDWR | O_CLOEXEC); 163 break; 164 } 165 } 166 drmFreeDevices(devices, ret); 167 168 if (fd == -1) 169 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, 170 "[drm] Failed to open DRM device for %s: %s\n", 171 pAMDGPUEnt->busid, strerror(errno)); 172 return fd; 173#undef MAX_DRM_DEVICES 174} 175 176void amdgpu_kernel_close_fd(AMDGPUEntPtr pAMDGPUEnt) 177{ 178#if defined(XSERVER_PLATFORM_BUS) && defined(XF86_PDEV_SERVER_FD) 179 if (!(pAMDGPUEnt->platform_dev && 180 pAMDGPUEnt->platform_dev->flags & XF86_PDEV_SERVER_FD)) 181#endif 182 close(pAMDGPUEnt->fd); 183 pAMDGPUEnt->fd = -1; 184} 185 186/* Pull a local version of the helper. It's available since 2.4.98 yet 187 * it may be too new for some distributions. 188 */ 189static int local_drmIsMaster(int fd) 190{ 191 return drmAuthMagic(fd, 0) != -EACCES; 192} 193 194static Bool amdgpu_open_drm_master(ScrnInfoPtr pScrn, 195 struct pci_device *pci_dev, 196 struct xf86_platform_device *platform_dev, 197 AMDGPUEntPtr pAMDGPUEnt) 198{ 199 pAMDGPUEnt->fd = amdgpu_kernel_open_fd(pScrn, pci_dev, platform_dev, pAMDGPUEnt); 200 if (pAMDGPUEnt->fd == -1) 201 return FALSE; 202 203 /* Check that what we opened is a master or a master-capable FD */ 204 if (!local_drmIsMaster(pAMDGPUEnt->fd)) { 205 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, 206 "[drm] device is not DRM master.\n"); 207 amdgpu_kernel_close_fd(pAMDGPUEnt); 208 return FALSE; 209 } 210 211 return TRUE; 212} 213 214static Bool amdgpu_device_setup(ScrnInfoPtr pScrn, 215 struct pci_device *pci_dev, 216 struct xf86_platform_device *platform_dev, 217 AMDGPUEntPtr pAMDGPUEnt) 218{ 219 uint32_t major_version; 220 uint32_t minor_version; 221 222 pAMDGPUEnt->platform_dev = platform_dev; 223 if (!amdgpu_open_drm_master(pScrn, pci_dev, platform_dev, 224 pAMDGPUEnt)) 225 return FALSE; 226 227 if (amdgpu_device_initialize(pAMDGPUEnt->fd, 228 &major_version, 229 &minor_version, 230 &pAMDGPUEnt->pDev)) { 231 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, 232 "amdgpu_device_initialize failed\n"); 233 goto error_amdgpu; 234 } 235 236 return TRUE; 237 238error_amdgpu: 239 amdgpu_kernel_close_fd(pAMDGPUEnt); 240 return FALSE; 241} 242 243static Bool 244amdgpu_probe(ScrnInfoPtr pScrn, int entity_num, 245 struct pci_device *pci_dev, struct xf86_platform_device *dev) 246{ 247 EntityInfoPtr pEnt = NULL; 248 DevUnion *pPriv; 249 AMDGPUEntPtr pAMDGPUEnt; 250 251 if (!pScrn) 252 return FALSE; 253 254 pScrn->driverVersion = AMDGPU_VERSION_CURRENT; 255 pScrn->driverName = AMDGPU_DRIVER_NAME; 256 pScrn->name = AMDGPU_NAME; 257 pScrn->Probe = NULL; 258 pScrn->PreInit = AMDGPUPreInit_KMS; 259 pScrn->ScreenInit = AMDGPUScreenInit_KMS; 260 pScrn->SwitchMode = AMDGPUSwitchMode_KMS; 261 pScrn->AdjustFrame = AMDGPUAdjustFrame_KMS; 262 pScrn->EnterVT = AMDGPUEnterVT_KMS; 263 pScrn->LeaveVT = AMDGPULeaveVT_KMS; 264 pScrn->FreeScreen = AMDGPUFreeScreen_KMS; 265 pScrn->ValidMode = AMDGPUValidMode; 266 267 pEnt = xf86GetEntityInfo(entity_num); 268 269 /* Create a AMDGPUEntity for all chips, even with old single head 270 * Radeon, need to use pAMDGPUEnt for new monitor detection routines. 271 */ 272 xf86SetEntitySharable(entity_num); 273 274 if (gAMDGPUEntityIndex == -1) 275 gAMDGPUEntityIndex = xf86AllocateEntityPrivateIndex(); 276 277 pPriv = xf86GetEntityPrivate(pEnt->index, gAMDGPUEntityIndex); 278 279 if (!pPriv->ptr) { 280 pPriv->ptr = xnfcalloc(sizeof(AMDGPUEntRec), 1); 281 if (!pPriv->ptr) 282 goto error; 283 284 pAMDGPUEnt = pPriv->ptr; 285 if (!amdgpu_device_setup(pScrn, pci_dev, dev, pAMDGPUEnt)) 286 goto error; 287 288 pAMDGPUEnt->fd_ref = 1; 289 290 } else { 291 pAMDGPUEnt = pPriv->ptr; 292 293 if (pAMDGPUEnt->fd_ref == ARRAY_SIZE(pAMDGPUEnt->scrn)) { 294 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, 295 "Only up to %u Zaphod instances supported\n", 296 (unsigned)ARRAY_SIZE(pAMDGPUEnt->scrn)); 297 goto error; 298 } 299 300 pAMDGPUEnt->fd_ref++; 301 } 302 303 xf86SetEntityInstanceForScreen(pScrn, pEnt->index, 304 xf86GetNumEntityInstances(pEnt-> 305 index) 306 - 1); 307 free(pEnt); 308 309 return TRUE; 310 311error: 312 free(pEnt); 313 return FALSE; 314} 315 316static Bool 317amdgpu_pci_probe(DriverPtr pDriver, 318 int entity_num, struct pci_device *device, intptr_t match_data) 319{ 320 ScrnInfoPtr pScrn = xf86ConfigPciEntity(NULL, 0, entity_num, NULL, 321 NULL, NULL, NULL, NULL, NULL); 322 323 return amdgpu_probe(pScrn, entity_num, device, NULL); 324} 325 326static Bool AMDGPUDriverFunc(ScrnInfoPtr scrn, xorgDriverFuncOp op, void *data) 327{ 328 xorgHWFlags *flag; 329 330 switch (op) { 331 case GET_REQUIRED_HW_INTERFACES: 332 flag = (CARD32 *) data; 333 (*flag) = 0; 334 return TRUE; 335#if XORG_VERSION_CURRENT > XORG_VERSION_NUMERIC(1,15,99,0,0) 336 case SUPPORTS_SERVER_FDS: 337 return TRUE; 338#endif 339 default: 340 return FALSE; 341 } 342} 343 344#ifdef XSERVER_PLATFORM_BUS 345static Bool 346amdgpu_platform_probe(DriverPtr pDriver, 347 int entity_num, int flags, 348 struct xf86_platform_device *dev, intptr_t match_data) 349{ 350 ScrnInfoPtr pScrn; 351 int scr_flags = 0; 352 353 if (!dev->pdev) 354 return FALSE; 355 356 if (flags & PLATFORM_PROBE_GPU_SCREEN) 357 scr_flags = XF86_ALLOCATE_GPU_SCREEN; 358 359 pScrn = xf86AllocateScreen(pDriver, scr_flags); 360 if (xf86IsEntitySharable(entity_num)) 361 xf86SetEntityShared(entity_num); 362 xf86AddEntityToScreen(pScrn, entity_num); 363 364 return amdgpu_probe(pScrn, entity_num, NULL, dev); 365} 366#endif 367 368static const struct pci_id_match amdgpu_device_match[] = { 369 {0x1002, PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY, 0, 0, 0}, 370 {0, 0, 0}, 371}; 372 373DriverRec AMDGPU = { 374 AMDGPU_VERSION_CURRENT, 375 AMDGPU_DRIVER_NAME, 376 AMDGPUIdentify, 377 NULL, 378 AMDGPUAvailableOptions, 379 NULL, 380 0, 381 AMDGPUDriverFunc, 382 amdgpu_device_match, 383 amdgpu_pci_probe, 384#ifdef XSERVER_PLATFORM_BUS 385 amdgpu_platform_probe 386#endif 387}; 388