nouveau_drm.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include <linux/delay.h>
  25. #include <linux/module.h>
  26. #include <linux/pci.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/vga_switcheroo.h>
  29. #include <linux/mmu_notifier.h>
  30. #include <linux/dynamic_debug.h>
  31. #include <drm/drm_aperture.h>
  32. #include <drm/drm_crtc_helper.h>
  33. #include <drm/drm_drv.h>
  34. #include <drm/drm_gem_ttm_helper.h>
  35. #include <drm/drm_ioctl.h>
  36. #include <drm/drm_vblank.h>
  37. #include <core/gpuobj.h>
  38. #include <core/option.h>
  39. #include <core/pci.h>
  40. #include <core/tegra.h>
  41. #include <nvif/driver.h>
  42. #include <nvif/fifo.h>
  43. #include <nvif/push006c.h>
  44. #include <nvif/user.h>
  45. #include <nvif/class.h>
  46. #include <nvif/cl0002.h>
  47. #include <nvif/cla06f.h>
  48. #include "nouveau_drv.h"
  49. #include "nouveau_dma.h"
  50. #include "nouveau_ttm.h"
  51. #include "nouveau_gem.h"
  52. #include "nouveau_vga.h"
  53. #include "nouveau_led.h"
  54. #include "nouveau_hwmon.h"
  55. #include "nouveau_acpi.h"
  56. #include "nouveau_bios.h"
  57. #include "nouveau_ioctl.h"
  58. #include "nouveau_abi16.h"
  59. #include "nouveau_fbcon.h"
  60. #include "nouveau_fence.h"
  61. #include "nouveau_debugfs.h"
  62. #include "nouveau_usif.h"
  63. #include "nouveau_connector.h"
  64. #include "nouveau_platform.h"
  65. #include "nouveau_svm.h"
  66. #include "nouveau_dmem.h"
  67. DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0,
  68. "DRM_UT_CORE",
  69. "DRM_UT_DRIVER",
  70. "DRM_UT_KMS",
  71. "DRM_UT_PRIME",
  72. "DRM_UT_ATOMIC",
  73. "DRM_UT_VBL",
  74. "DRM_UT_STATE",
  75. "DRM_UT_LEASE",
  76. "DRM_UT_DP",
  77. "DRM_UT_DRMRES");
  78. MODULE_PARM_DESC(config, "option string to pass to driver core");
  79. static char *nouveau_config;
  80. module_param_named(config, nouveau_config, charp, 0400);
  81. MODULE_PARM_DESC(debug, "debug string to pass to driver core");
  82. static char *nouveau_debug;
  83. module_param_named(debug, nouveau_debug, charp, 0400);
  84. MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
  85. static int nouveau_noaccel = 0;
  86. module_param_named(noaccel, nouveau_noaccel, int, 0400);
  87. MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
  88. "0 = disabled, 1 = enabled, 2 = headless)");
  89. int nouveau_modeset = -1;
  90. module_param_named(modeset, nouveau_modeset, int, 0400);
  91. MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
  92. static int nouveau_atomic = 0;
  93. module_param_named(atomic, nouveau_atomic, int, 0400);
  94. MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
  95. static int nouveau_runtime_pm = -1;
  96. module_param_named(runpm, nouveau_runtime_pm, int, 0400);
  97. static struct drm_driver driver_stub;
  98. static struct drm_driver driver_pci;
  99. static struct drm_driver driver_platform;
  100. static u64
  101. nouveau_pci_name(struct pci_dev *pdev)
  102. {
  103. u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
  104. name |= pdev->bus->number << 16;
  105. name |= PCI_SLOT(pdev->devfn) << 8;
  106. return name | PCI_FUNC(pdev->devfn);
  107. }
  108. static u64
  109. nouveau_platform_name(struct platform_device *platformdev)
  110. {
  111. return platformdev->id;
  112. }
  113. static u64
  114. nouveau_name(struct drm_device *dev)
  115. {
  116. if (dev_is_pci(dev->dev))
  117. return nouveau_pci_name(to_pci_dev(dev->dev));
  118. else
  119. return nouveau_platform_name(to_platform_device(dev->dev));
  120. }
  121. static inline bool
  122. nouveau_cli_work_ready(struct dma_fence *fence)
  123. {
  124. bool ret = true;
  125. spin_lock_irq(fence->lock);
  126. if (!dma_fence_is_signaled_locked(fence))
  127. ret = false;
  128. spin_unlock_irq(fence->lock);
  129. if (ret == true)
  130. dma_fence_put(fence);
  131. return ret;
  132. }
  133. static void
  134. nouveau_cli_work(struct work_struct *w)
  135. {
  136. struct nouveau_cli *cli = container_of(w, typeof(*cli), work);
  137. struct nouveau_cli_work *work, *wtmp;
  138. mutex_lock(&cli->lock);
  139. list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
  140. if (!work->fence || nouveau_cli_work_ready(work->fence)) {
  141. list_del(&work->head);
  142. work->func(work);
  143. }
  144. }
  145. mutex_unlock(&cli->lock);
  146. }
  147. static void
  148. nouveau_cli_work_fence(struct dma_fence *fence, struct dma_fence_cb *cb)
  149. {
  150. struct nouveau_cli_work *work = container_of(cb, typeof(*work), cb);
  151. schedule_work(&work->cli->work);
  152. }
  153. void
  154. nouveau_cli_work_queue(struct nouveau_cli *cli, struct dma_fence *fence,
  155. struct nouveau_cli_work *work)
  156. {
  157. work->fence = dma_fence_get(fence);
  158. work->cli = cli;
  159. mutex_lock(&cli->lock);
  160. list_add_tail(&work->head, &cli->worker);
  161. if (dma_fence_add_callback(fence, &work->cb, nouveau_cli_work_fence))
  162. nouveau_cli_work_fence(fence, &work->cb);
  163. mutex_unlock(&cli->lock);
  164. }
  165. static void
  166. nouveau_cli_fini(struct nouveau_cli *cli)
  167. {
  168. /* All our channels are dead now, which means all the fences they
  169. * own are signalled, and all callback functions have been called.
  170. *
  171. * So, after flushing the workqueue, there should be nothing left.
  172. */
  173. flush_work(&cli->work);
  174. WARN_ON(!list_empty(&cli->worker));
  175. usif_client_fini(cli);
  176. nouveau_vmm_fini(&cli->svm);
  177. nouveau_vmm_fini(&cli->vmm);
  178. nvif_mmu_dtor(&cli->mmu);
  179. nvif_device_dtor(&cli->device);
  180. mutex_lock(&cli->drm->master.lock);
  181. nvif_client_dtor(&cli->base);
  182. mutex_unlock(&cli->drm->master.lock);
  183. }
  184. static int
  185. nouveau_cli_init(struct nouveau_drm *drm, const char *sname,
  186. struct nouveau_cli *cli)
  187. {
  188. static const struct nvif_mclass
  189. mems[] = {
  190. { NVIF_CLASS_MEM_GF100, -1 },
  191. { NVIF_CLASS_MEM_NV50 , -1 },
  192. { NVIF_CLASS_MEM_NV04 , -1 },
  193. {}
  194. };
  195. static const struct nvif_mclass
  196. mmus[] = {
  197. { NVIF_CLASS_MMU_GF100, -1 },
  198. { NVIF_CLASS_MMU_NV50 , -1 },
  199. { NVIF_CLASS_MMU_NV04 , -1 },
  200. {}
  201. };
  202. static const struct nvif_mclass
  203. vmms[] = {
  204. { NVIF_CLASS_VMM_GP100, -1 },
  205. { NVIF_CLASS_VMM_GM200, -1 },
  206. { NVIF_CLASS_VMM_GF100, -1 },
  207. { NVIF_CLASS_VMM_NV50 , -1 },
  208. { NVIF_CLASS_VMM_NV04 , -1 },
  209. {}
  210. };
  211. u64 device = nouveau_name(drm->dev);
  212. int ret;
  213. snprintf(cli->name, sizeof(cli->name), "%s", sname);
  214. cli->drm = drm;
  215. mutex_init(&cli->mutex);
  216. usif_client_init(cli);
  217. INIT_WORK(&cli->work, nouveau_cli_work);
  218. INIT_LIST_HEAD(&cli->worker);
  219. mutex_init(&cli->lock);
  220. if (cli == &drm->master) {
  221. ret = nvif_driver_init(NULL, nouveau_config, nouveau_debug,
  222. cli->name, device, &cli->base);
  223. } else {
  224. mutex_lock(&drm->master.lock);
  225. ret = nvif_client_ctor(&drm->master.base, cli->name, device,
  226. &cli->base);
  227. mutex_unlock(&drm->master.lock);
  228. }
  229. if (ret) {
  230. NV_PRINTK(err, cli, "Client allocation failed: %d\n", ret);
  231. goto done;
  232. }
  233. ret = nvif_device_ctor(&cli->base.object, "drmDevice", 0, NV_DEVICE,
  234. &(struct nv_device_v0) {
  235. .device = ~0,
  236. .priv = true,
  237. }, sizeof(struct nv_device_v0),
  238. &cli->device);
  239. if (ret) {
  240. NV_PRINTK(err, cli, "Device allocation failed: %d\n", ret);
  241. goto done;
  242. }
  243. ret = nvif_mclass(&cli->device.object, mmus);
  244. if (ret < 0) {
  245. NV_PRINTK(err, cli, "No supported MMU class\n");
  246. goto done;
  247. }
  248. ret = nvif_mmu_ctor(&cli->device.object, "drmMmu", mmus[ret].oclass,
  249. &cli->mmu);
  250. if (ret) {
  251. NV_PRINTK(err, cli, "MMU allocation failed: %d\n", ret);
  252. goto done;
  253. }
  254. ret = nvif_mclass(&cli->mmu.object, vmms);
  255. if (ret < 0) {
  256. NV_PRINTK(err, cli, "No supported VMM class\n");
  257. goto done;
  258. }
  259. ret = nouveau_vmm_init(cli, vmms[ret].oclass, &cli->vmm);
  260. if (ret) {
  261. NV_PRINTK(err, cli, "VMM allocation failed: %d\n", ret);
  262. goto done;
  263. }
  264. ret = nvif_mclass(&cli->mmu.object, mems);
  265. if (ret < 0) {
  266. NV_PRINTK(err, cli, "No supported MEM class\n");
  267. goto done;
  268. }
  269. cli->mem = &mems[ret];
  270. return 0;
  271. done:
  272. if (ret)
  273. nouveau_cli_fini(cli);
  274. return ret;
  275. }
  276. static void
  277. nouveau_accel_ce_fini(struct nouveau_drm *drm)
  278. {
  279. nouveau_channel_idle(drm->cechan);
  280. nvif_object_dtor(&drm->ttm.copy);
  281. nouveau_channel_del(&drm->cechan);
  282. }
  283. static void
  284. nouveau_accel_ce_init(struct nouveau_drm *drm)
  285. {
  286. struct nvif_device *device = &drm->client.device;
  287. int ret = 0;
  288. /* Allocate channel that has access to a (preferably async) copy
  289. * engine, to use for TTM buffer moves.
  290. */
  291. if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
  292. ret = nouveau_channel_new(drm, device,
  293. nvif_fifo_runlist_ce(device), 0,
  294. true, &drm->cechan);
  295. } else
  296. if (device->info.chipset >= 0xa3 &&
  297. device->info.chipset != 0xaa &&
  298. device->info.chipset != 0xac) {
  299. /* Prior to Kepler, there's only a single runlist, so all
  300. * engines can be accessed from any channel.
  301. *
  302. * We still want to use a separate channel though.
  303. */
  304. ret = nouveau_channel_new(drm, device, NvDmaFB, NvDmaTT, false,
  305. &drm->cechan);
  306. }
  307. if (ret)
  308. NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
  309. }
  310. static void
  311. nouveau_accel_gr_fini(struct nouveau_drm *drm)
  312. {
  313. nouveau_channel_idle(drm->channel);
  314. nvif_object_dtor(&drm->ntfy);
  315. nvkm_gpuobj_del(&drm->notify);
  316. nouveau_channel_del(&drm->channel);
  317. }
  318. static void
  319. nouveau_accel_gr_init(struct nouveau_drm *drm)
  320. {
  321. struct nvif_device *device = &drm->client.device;
  322. u32 arg0, arg1;
  323. int ret;
  324. if (device->info.family >= NV_DEVICE_INFO_V0_AMPERE)
  325. return;
  326. /* Allocate channel that has access to the graphics engine. */
  327. if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
  328. arg0 = nvif_fifo_runlist(device, NV_DEVICE_HOST_RUNLIST_ENGINES_GR);
  329. arg1 = 1;
  330. } else {
  331. arg0 = NvDmaFB;
  332. arg1 = NvDmaTT;
  333. }
  334. ret = nouveau_channel_new(drm, device, arg0, arg1, false,
  335. &drm->channel);
  336. if (ret) {
  337. NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
  338. nouveau_accel_gr_fini(drm);
  339. return;
  340. }
  341. /* A SW class is used on pre-NV50 HW to assist with handling the
  342. * synchronisation of page flips, as well as to implement fences
  343. * on TNT/TNT2 HW that lacks any kind of support in host.
  344. */
  345. if (!drm->channel->nvsw.client && device->info.family < NV_DEVICE_INFO_V0_TESLA) {
  346. ret = nvif_object_ctor(&drm->channel->user, "drmNvsw",
  347. NVDRM_NVSW, nouveau_abi16_swclass(drm),
  348. NULL, 0, &drm->channel->nvsw);
  349. if (ret == 0) {
  350. struct nvif_push *push = drm->channel->chan.push;
  351. ret = PUSH_WAIT(push, 2);
  352. if (ret == 0)
  353. PUSH_NVSQ(push, NV_SW, 0x0000, drm->channel->nvsw.handle);
  354. }
  355. if (ret) {
  356. NV_ERROR(drm, "failed to allocate sw class, %d\n", ret);
  357. nouveau_accel_gr_fini(drm);
  358. return;
  359. }
  360. }
  361. /* NvMemoryToMemoryFormat requires a notifier ctxdma for some reason,
  362. * even if notification is never requested, so, allocate a ctxdma on
  363. * any GPU where it's possible we'll end up using M2MF for BO moves.
  364. */
  365. if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
  366. ret = nvkm_gpuobj_new(nvxx_device(device), 32, 0, false, NULL,
  367. &drm->notify);
  368. if (ret) {
  369. NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
  370. nouveau_accel_gr_fini(drm);
  371. return;
  372. }
  373. ret = nvif_object_ctor(&drm->channel->user, "drmM2mfNtfy",
  374. NvNotify0, NV_DMA_IN_MEMORY,
  375. &(struct nv_dma_v0) {
  376. .target = NV_DMA_V0_TARGET_VRAM,
  377. .access = NV_DMA_V0_ACCESS_RDWR,
  378. .start = drm->notify->addr,
  379. .limit = drm->notify->addr + 31
  380. }, sizeof(struct nv_dma_v0),
  381. &drm->ntfy);
  382. if (ret) {
  383. nouveau_accel_gr_fini(drm);
  384. return;
  385. }
  386. }
  387. }
  388. static void
  389. nouveau_accel_fini(struct nouveau_drm *drm)
  390. {
  391. nouveau_accel_ce_fini(drm);
  392. nouveau_accel_gr_fini(drm);
  393. if (drm->fence)
  394. nouveau_fence(drm)->dtor(drm);
  395. }
  396. static void
  397. nouveau_accel_init(struct nouveau_drm *drm)
  398. {
  399. struct nvif_device *device = &drm->client.device;
  400. struct nvif_sclass *sclass;
  401. int ret, i, n;
  402. if (nouveau_noaccel)
  403. return;
  404. /* Initialise global support for channels, and synchronisation. */
  405. ret = nouveau_channels_init(drm);
  406. if (ret)
  407. return;
  408. /*XXX: this is crap, but the fence/channel stuff is a little
  409. * backwards in some places. this will be fixed.
  410. */
  411. ret = n = nvif_object_sclass_get(&device->object, &sclass);
  412. if (ret < 0)
  413. return;
  414. for (ret = -ENOSYS, i = 0; i < n; i++) {
  415. switch (sclass[i].oclass) {
  416. case NV03_CHANNEL_DMA:
  417. ret = nv04_fence_create(drm);
  418. break;
  419. case NV10_CHANNEL_DMA:
  420. ret = nv10_fence_create(drm);
  421. break;
  422. case NV17_CHANNEL_DMA:
  423. case NV40_CHANNEL_DMA:
  424. ret = nv17_fence_create(drm);
  425. break;
  426. case NV50_CHANNEL_GPFIFO:
  427. ret = nv50_fence_create(drm);
  428. break;
  429. case G82_CHANNEL_GPFIFO:
  430. ret = nv84_fence_create(drm);
  431. break;
  432. case FERMI_CHANNEL_GPFIFO:
  433. case KEPLER_CHANNEL_GPFIFO_A:
  434. case KEPLER_CHANNEL_GPFIFO_B:
  435. case MAXWELL_CHANNEL_GPFIFO_A:
  436. case PASCAL_CHANNEL_GPFIFO_A:
  437. case VOLTA_CHANNEL_GPFIFO_A:
  438. case TURING_CHANNEL_GPFIFO_A:
  439. case AMPERE_CHANNEL_GPFIFO_B:
  440. ret = nvc0_fence_create(drm);
  441. break;
  442. default:
  443. break;
  444. }
  445. }
  446. nvif_object_sclass_put(&sclass);
  447. if (ret) {
  448. NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
  449. nouveau_accel_fini(drm);
  450. return;
  451. }
  452. /* Volta requires access to a doorbell register for kickoff. */
  453. if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_VOLTA) {
  454. ret = nvif_user_ctor(device, "drmUsermode");
  455. if (ret)
  456. return;
  457. }
  458. /* Allocate channels we need to support various functions. */
  459. nouveau_accel_gr_init(drm);
  460. nouveau_accel_ce_init(drm);
  461. /* Initialise accelerated TTM buffer moves. */
  462. nouveau_bo_move_init(drm);
  463. }
  464. static void __printf(2, 3)
  465. nouveau_drm_errorf(struct nvif_object *object, const char *fmt, ...)
  466. {
  467. struct nouveau_drm *drm = container_of(object->parent, typeof(*drm), parent);
  468. struct va_format vaf;
  469. va_list va;
  470. va_start(va, fmt);
  471. vaf.fmt = fmt;
  472. vaf.va = &va;
  473. NV_ERROR(drm, "%pV", &vaf);
  474. va_end(va);
  475. }
  476. static void __printf(2, 3)
  477. nouveau_drm_debugf(struct nvif_object *object, const char *fmt, ...)
  478. {
  479. struct nouveau_drm *drm = container_of(object->parent, typeof(*drm), parent);
  480. struct va_format vaf;
  481. va_list va;
  482. va_start(va, fmt);
  483. vaf.fmt = fmt;
  484. vaf.va = &va;
  485. NV_DEBUG(drm, "%pV", &vaf);
  486. va_end(va);
  487. }
  488. static const struct nvif_parent_func
  489. nouveau_parent = {
  490. .debugf = nouveau_drm_debugf,
  491. .errorf = nouveau_drm_errorf,
  492. };
  493. static int
  494. nouveau_drm_device_init(struct drm_device *dev)
  495. {
  496. struct nouveau_drm *drm;
  497. int ret;
  498. if (!(drm = kzalloc(sizeof(*drm), GFP_KERNEL)))
  499. return -ENOMEM;
  500. dev->dev_private = drm;
  501. drm->dev = dev;
  502. nvif_parent_ctor(&nouveau_parent, &drm->parent);
  503. drm->master.base.object.parent = &drm->parent;
  504. ret = nouveau_cli_init(drm, "DRM-master", &drm->master);
  505. if (ret)
  506. goto fail_alloc;
  507. ret = nouveau_cli_init(drm, "DRM", &drm->client);
  508. if (ret)
  509. goto fail_master;
  510. nvxx_client(&drm->client.base)->debug =
  511. nvkm_dbgopt(nouveau_debug, "DRM");
  512. INIT_LIST_HEAD(&drm->clients);
  513. mutex_init(&drm->clients_lock);
  514. spin_lock_init(&drm->tile.lock);
  515. /* workaround an odd issue on nvc1 by disabling the device's
  516. * nosnoop capability. hopefully won't cause issues until a
  517. * better fix is found - assuming there is one...
  518. */
  519. if (drm->client.device.info.chipset == 0xc1)
  520. nvif_mask(&drm->client.device.object, 0x00088080, 0x00000800, 0x00000000);
  521. nouveau_vga_init(drm);
  522. ret = nouveau_ttm_init(drm);
  523. if (ret)
  524. goto fail_ttm;
  525. ret = nouveau_bios_init(dev);
  526. if (ret)
  527. goto fail_bios;
  528. nouveau_accel_init(drm);
  529. ret = nouveau_display_create(dev);
  530. if (ret)
  531. goto fail_dispctor;
  532. if (dev->mode_config.num_crtc) {
  533. ret = nouveau_display_init(dev, false, false);
  534. if (ret)
  535. goto fail_dispinit;
  536. }
  537. nouveau_debugfs_init(drm);
  538. nouveau_hwmon_init(dev);
  539. nouveau_svm_init(drm);
  540. nouveau_dmem_init(drm);
  541. nouveau_fbcon_init(dev);
  542. nouveau_led_init(dev);
  543. if (nouveau_pmops_runtime()) {
  544. pm_runtime_use_autosuspend(dev->dev);
  545. pm_runtime_set_autosuspend_delay(dev->dev, 5000);
  546. pm_runtime_set_active(dev->dev);
  547. pm_runtime_allow(dev->dev);
  548. pm_runtime_mark_last_busy(dev->dev);
  549. pm_runtime_put(dev->dev);
  550. }
  551. return 0;
  552. fail_dispinit:
  553. nouveau_display_destroy(dev);
  554. fail_dispctor:
  555. nouveau_accel_fini(drm);
  556. nouveau_bios_takedown(dev);
  557. fail_bios:
  558. nouveau_ttm_fini(drm);
  559. fail_ttm:
  560. nouveau_vga_fini(drm);
  561. nouveau_cli_fini(&drm->client);
  562. fail_master:
  563. nouveau_cli_fini(&drm->master);
  564. fail_alloc:
  565. nvif_parent_dtor(&drm->parent);
  566. kfree(drm);
  567. return ret;
  568. }
  569. static void
  570. nouveau_drm_device_fini(struct drm_device *dev)
  571. {
  572. struct nouveau_cli *cli, *temp_cli;
  573. struct nouveau_drm *drm = nouveau_drm(dev);
  574. if (nouveau_pmops_runtime()) {
  575. pm_runtime_get_sync(dev->dev);
  576. pm_runtime_forbid(dev->dev);
  577. }
  578. nouveau_led_fini(dev);
  579. nouveau_fbcon_fini(dev);
  580. nouveau_dmem_fini(drm);
  581. nouveau_svm_fini(drm);
  582. nouveau_hwmon_fini(dev);
  583. nouveau_debugfs_fini(drm);
  584. if (dev->mode_config.num_crtc)
  585. nouveau_display_fini(dev, false, false);
  586. nouveau_display_destroy(dev);
  587. nouveau_accel_fini(drm);
  588. nouveau_bios_takedown(dev);
  589. nouveau_ttm_fini(drm);
  590. nouveau_vga_fini(drm);
  591. /*
  592. * There may be existing clients from as-yet unclosed files. For now,
  593. * clean them up here rather than deferring until the file is closed,
  594. * but this likely not correct if we want to support hot-unplugging
  595. * properly.
  596. */
  597. mutex_lock(&drm->clients_lock);
  598. list_for_each_entry_safe(cli, temp_cli, &drm->clients, head) {
  599. list_del(&cli->head);
  600. mutex_lock(&cli->mutex);
  601. if (cli->abi16)
  602. nouveau_abi16_fini(cli->abi16);
  603. mutex_unlock(&cli->mutex);
  604. nouveau_cli_fini(cli);
  605. kfree(cli);
  606. }
  607. mutex_unlock(&drm->clients_lock);
  608. nouveau_cli_fini(&drm->client);
  609. nouveau_cli_fini(&drm->master);
  610. nvif_parent_dtor(&drm->parent);
  611. mutex_destroy(&drm->clients_lock);
  612. kfree(drm);
  613. }
  614. /*
  615. * On some Intel PCIe bridge controllers doing a
  616. * D0 -> D3hot -> D3cold -> D0 sequence causes Nvidia GPUs to not reappear.
  617. * Skipping the intermediate D3hot step seems to make it work again. This is
  618. * probably caused by not meeting the expectation the involved AML code has
  619. * when the GPU is put into D3hot state before invoking it.
  620. *
  621. * This leads to various manifestations of this issue:
  622. * - AML code execution to power on the GPU hits an infinite loop (as the
  623. * code waits on device memory to change).
  624. * - kernel crashes, as all PCI reads return -1, which most code isn't able
  625. * to handle well enough.
  626. *
  627. * In all cases dmesg will contain at least one line like this:
  628. * 'nouveau 0000:01:00.0: Refused to change power state, currently in D3'
  629. * followed by a lot of nouveau timeouts.
  630. *
  631. * In the \_SB.PCI0.PEG0.PG00._OFF code deeper down writes bit 0x80 to the not
  632. * documented PCI config space register 0x248 of the Intel PCIe bridge
  633. * controller (0x1901) in order to change the state of the PCIe link between
  634. * the PCIe port and the GPU. There are alternative code paths using other
  635. * registers, which seem to work fine (executed pre Windows 8):
  636. * - 0xbc bit 0x20 (publicly available documentation claims 'reserved')
  637. * - 0xb0 bit 0x10 (link disable)
  638. * Changing the conditions inside the firmware by poking into the relevant
  639. * addresses does resolve the issue, but it seemed to be ACPI private memory
  640. * and not any device accessible memory at all, so there is no portable way of
  641. * changing the conditions.
  642. * On a XPS 9560 that means bits [0,3] on \CPEX need to be cleared.
  643. *
  644. * The only systems where this behavior can be seen are hybrid graphics laptops
  645. * with a secondary Nvidia Maxwell, Pascal or Turing GPU. It's unclear whether
  646. * this issue only occurs in combination with listed Intel PCIe bridge
  647. * controllers and the mentioned GPUs or other devices as well.
  648. *
  649. * documentation on the PCIe bridge controller can be found in the
  650. * "7th Generation Intel® Processor Families for H Platforms Datasheet Volume 2"
  651. * Section "12 PCI Express* Controller (x16) Registers"
  652. */
  653. static void quirk_broken_nv_runpm(struct pci_dev *pdev)
  654. {
  655. struct drm_device *dev = pci_get_drvdata(pdev);
  656. struct nouveau_drm *drm = nouveau_drm(dev);
  657. struct pci_dev *bridge = pci_upstream_bridge(pdev);
  658. if (!bridge || bridge->vendor != PCI_VENDOR_ID_INTEL)
  659. return;
  660. switch (bridge->device) {
  661. case 0x1901:
  662. drm->old_pm_cap = pdev->pm_cap;
  663. pdev->pm_cap = 0;
  664. NV_INFO(drm, "Disabling PCI power management to avoid bug\n");
  665. break;
  666. }
  667. }
  668. static int nouveau_drm_probe(struct pci_dev *pdev,
  669. const struct pci_device_id *pent)
  670. {
  671. struct nvkm_device *device;
  672. struct drm_device *drm_dev;
  673. int ret;
  674. if (vga_switcheroo_client_probe_defer(pdev))
  675. return -EPROBE_DEFER;
  676. /* We need to check that the chipset is supported before booting
  677. * fbdev off the hardware, as there's no way to put it back.
  678. */
  679. ret = nvkm_device_pci_new(pdev, nouveau_config, "error",
  680. true, false, 0, &device);
  681. if (ret)
  682. return ret;
  683. nvkm_device_del(&device);
  684. /* Remove conflicting drivers (vesafb, efifb etc). */
  685. ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver_pci);
  686. if (ret)
  687. return ret;
  688. ret = nvkm_device_pci_new(pdev, nouveau_config, nouveau_debug,
  689. true, true, ~0ULL, &device);
  690. if (ret)
  691. return ret;
  692. pci_set_master(pdev);
  693. if (nouveau_atomic)
  694. driver_pci.driver_features |= DRIVER_ATOMIC;
  695. drm_dev = drm_dev_alloc(&driver_pci, &pdev->dev);
  696. if (IS_ERR(drm_dev)) {
  697. ret = PTR_ERR(drm_dev);
  698. goto fail_nvkm;
  699. }
  700. ret = pci_enable_device(pdev);
  701. if (ret)
  702. goto fail_drm;
  703. pci_set_drvdata(pdev, drm_dev);
  704. ret = nouveau_drm_device_init(drm_dev);
  705. if (ret)
  706. goto fail_pci;
  707. ret = drm_dev_register(drm_dev, pent->driver_data);
  708. if (ret)
  709. goto fail_drm_dev_init;
  710. quirk_broken_nv_runpm(pdev);
  711. return 0;
  712. fail_drm_dev_init:
  713. nouveau_drm_device_fini(drm_dev);
  714. fail_pci:
  715. pci_disable_device(pdev);
  716. fail_drm:
  717. drm_dev_put(drm_dev);
  718. fail_nvkm:
  719. nvkm_device_del(&device);
  720. return ret;
  721. }
  722. void
  723. nouveau_drm_device_remove(struct drm_device *dev)
  724. {
  725. struct nouveau_drm *drm = nouveau_drm(dev);
  726. struct nvkm_client *client;
  727. struct nvkm_device *device;
  728. drm_dev_unplug(dev);
  729. client = nvxx_client(&drm->client.base);
  730. device = nvkm_device_find(client->device);
  731. nouveau_drm_device_fini(dev);
  732. drm_dev_put(dev);
  733. nvkm_device_del(&device);
  734. }
  735. static void
  736. nouveau_drm_remove(struct pci_dev *pdev)
  737. {
  738. struct drm_device *dev = pci_get_drvdata(pdev);
  739. struct nouveau_drm *drm = nouveau_drm(dev);
  740. /* revert our workaround */
  741. if (drm->old_pm_cap)
  742. pdev->pm_cap = drm->old_pm_cap;
  743. nouveau_drm_device_remove(dev);
  744. pci_disable_device(pdev);
  745. }
  746. static int
  747. nouveau_do_suspend(struct drm_device *dev, bool runtime)
  748. {
  749. struct nouveau_drm *drm = nouveau_drm(dev);
  750. struct ttm_resource_manager *man;
  751. int ret;
  752. nouveau_svm_suspend(drm);
  753. nouveau_dmem_suspend(drm);
  754. nouveau_led_suspend(dev);
  755. if (dev->mode_config.num_crtc) {
  756. NV_DEBUG(drm, "suspending console...\n");
  757. nouveau_fbcon_set_suspend(dev, 1);
  758. NV_DEBUG(drm, "suspending display...\n");
  759. ret = nouveau_display_suspend(dev, runtime);
  760. if (ret)
  761. return ret;
  762. }
  763. NV_DEBUG(drm, "evicting buffers...\n");
  764. man = ttm_manager_type(&drm->ttm.bdev, TTM_PL_VRAM);
  765. ttm_resource_manager_evict_all(&drm->ttm.bdev, man);
  766. NV_DEBUG(drm, "waiting for kernel channels to go idle...\n");
  767. if (drm->cechan) {
  768. ret = nouveau_channel_idle(drm->cechan);
  769. if (ret)
  770. goto fail_display;
  771. }
  772. if (drm->channel) {
  773. ret = nouveau_channel_idle(drm->channel);
  774. if (ret)
  775. goto fail_display;
  776. }
  777. NV_DEBUG(drm, "suspending fence...\n");
  778. if (drm->fence && nouveau_fence(drm)->suspend) {
  779. if (!nouveau_fence(drm)->suspend(drm)) {
  780. ret = -ENOMEM;
  781. goto fail_display;
  782. }
  783. }
  784. NV_DEBUG(drm, "suspending object tree...\n");
  785. ret = nvif_client_suspend(&drm->master.base);
  786. if (ret)
  787. goto fail_client;
  788. return 0;
  789. fail_client:
  790. if (drm->fence && nouveau_fence(drm)->resume)
  791. nouveau_fence(drm)->resume(drm);
  792. fail_display:
  793. if (dev->mode_config.num_crtc) {
  794. NV_DEBUG(drm, "resuming display...\n");
  795. nouveau_display_resume(dev, runtime);
  796. }
  797. return ret;
  798. }
  799. static int
  800. nouveau_do_resume(struct drm_device *dev, bool runtime)
  801. {
  802. int ret = 0;
  803. struct nouveau_drm *drm = nouveau_drm(dev);
  804. NV_DEBUG(drm, "resuming object tree...\n");
  805. ret = nvif_client_resume(&drm->master.base);
  806. if (ret) {
  807. NV_ERROR(drm, "Client resume failed with error: %d\n", ret);
  808. return ret;
  809. }
  810. NV_DEBUG(drm, "resuming fence...\n");
  811. if (drm->fence && nouveau_fence(drm)->resume)
  812. nouveau_fence(drm)->resume(drm);
  813. nouveau_run_vbios_init(dev);
  814. if (dev->mode_config.num_crtc) {
  815. NV_DEBUG(drm, "resuming display...\n");
  816. nouveau_display_resume(dev, runtime);
  817. NV_DEBUG(drm, "resuming console...\n");
  818. nouveau_fbcon_set_suspend(dev, 0);
  819. }
  820. nouveau_led_resume(dev);
  821. nouveau_dmem_resume(drm);
  822. nouveau_svm_resume(drm);
  823. return 0;
  824. }
  825. int
  826. nouveau_pmops_suspend(struct device *dev)
  827. {
  828. struct pci_dev *pdev = to_pci_dev(dev);
  829. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  830. int ret;
  831. if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
  832. drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
  833. return 0;
  834. ret = nouveau_do_suspend(drm_dev, false);
  835. if (ret)
  836. return ret;
  837. pci_save_state(pdev);
  838. pci_disable_device(pdev);
  839. pci_set_power_state(pdev, PCI_D3hot);
  840. udelay(200);
  841. return 0;
  842. }
  843. int
  844. nouveau_pmops_resume(struct device *dev)
  845. {
  846. struct pci_dev *pdev = to_pci_dev(dev);
  847. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  848. int ret;
  849. if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
  850. drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
  851. return 0;
  852. pci_set_power_state(pdev, PCI_D0);
  853. pci_restore_state(pdev);
  854. ret = pci_enable_device(pdev);
  855. if (ret)
  856. return ret;
  857. pci_set_master(pdev);
  858. ret = nouveau_do_resume(drm_dev, false);
  859. /* Monitors may have been connected / disconnected during suspend */
  860. nouveau_display_hpd_resume(drm_dev);
  861. return ret;
  862. }
  863. static int
  864. nouveau_pmops_freeze(struct device *dev)
  865. {
  866. struct pci_dev *pdev = to_pci_dev(dev);
  867. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  868. return nouveau_do_suspend(drm_dev, false);
  869. }
  870. static int
  871. nouveau_pmops_thaw(struct device *dev)
  872. {
  873. struct pci_dev *pdev = to_pci_dev(dev);
  874. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  875. return nouveau_do_resume(drm_dev, false);
  876. }
  877. bool
  878. nouveau_pmops_runtime(void)
  879. {
  880. if (nouveau_runtime_pm == -1)
  881. return nouveau_is_optimus() || nouveau_is_v1_dsm();
  882. return nouveau_runtime_pm == 1;
  883. }
  884. static int
  885. nouveau_pmops_runtime_suspend(struct device *dev)
  886. {
  887. struct pci_dev *pdev = to_pci_dev(dev);
  888. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  889. int ret;
  890. if (!nouveau_pmops_runtime()) {
  891. pm_runtime_forbid(dev);
  892. return -EBUSY;
  893. }
  894. nouveau_switcheroo_optimus_dsm();
  895. ret = nouveau_do_suspend(drm_dev, true);
  896. pci_save_state(pdev);
  897. pci_disable_device(pdev);
  898. pci_ignore_hotplug(pdev);
  899. pci_set_power_state(pdev, PCI_D3cold);
  900. drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
  901. return ret;
  902. }
  903. static int
  904. nouveau_pmops_runtime_resume(struct device *dev)
  905. {
  906. struct pci_dev *pdev = to_pci_dev(dev);
  907. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  908. struct nouveau_drm *drm = nouveau_drm(drm_dev);
  909. struct nvif_device *device = &nouveau_drm(drm_dev)->client.device;
  910. int ret;
  911. if (!nouveau_pmops_runtime()) {
  912. pm_runtime_forbid(dev);
  913. return -EBUSY;
  914. }
  915. pci_set_power_state(pdev, PCI_D0);
  916. pci_restore_state(pdev);
  917. ret = pci_enable_device(pdev);
  918. if (ret)
  919. return ret;
  920. pci_set_master(pdev);
  921. ret = nouveau_do_resume(drm_dev, true);
  922. if (ret) {
  923. NV_ERROR(drm, "resume failed with: %d\n", ret);
  924. return ret;
  925. }
  926. /* do magic */
  927. nvif_mask(&device->object, 0x088488, (1 << 25), (1 << 25));
  928. drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
  929. /* Monitors may have been connected / disconnected during suspend */
  930. nouveau_display_hpd_resume(drm_dev);
  931. return ret;
  932. }
  933. static int
  934. nouveau_pmops_runtime_idle(struct device *dev)
  935. {
  936. if (!nouveau_pmops_runtime()) {
  937. pm_runtime_forbid(dev);
  938. return -EBUSY;
  939. }
  940. pm_runtime_mark_last_busy(dev);
  941. pm_runtime_autosuspend(dev);
  942. /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
  943. return 1;
  944. }
  945. static int
  946. nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
  947. {
  948. struct nouveau_drm *drm = nouveau_drm(dev);
  949. struct nouveau_cli *cli;
  950. char name[32], tmpname[TASK_COMM_LEN];
  951. int ret;
  952. /* need to bring up power immediately if opening device */
  953. ret = pm_runtime_get_sync(dev->dev);
  954. if (ret < 0 && ret != -EACCES) {
  955. pm_runtime_put_autosuspend(dev->dev);
  956. return ret;
  957. }
  958. get_task_comm(tmpname, current);
  959. snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
  960. if (!(cli = kzalloc(sizeof(*cli), GFP_KERNEL))) {
  961. ret = -ENOMEM;
  962. goto done;
  963. }
  964. ret = nouveau_cli_init(drm, name, cli);
  965. if (ret)
  966. goto done;
  967. fpriv->driver_priv = cli;
  968. mutex_lock(&drm->clients_lock);
  969. list_add(&cli->head, &drm->clients);
  970. mutex_unlock(&drm->clients_lock);
  971. done:
  972. if (ret && cli) {
  973. nouveau_cli_fini(cli);
  974. kfree(cli);
  975. }
  976. pm_runtime_mark_last_busy(dev->dev);
  977. pm_runtime_put_autosuspend(dev->dev);
  978. return ret;
  979. }
  980. static void
  981. nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
  982. {
  983. struct nouveau_cli *cli = nouveau_cli(fpriv);
  984. struct nouveau_drm *drm = nouveau_drm(dev);
  985. int dev_index;
  986. /*
  987. * The device is gone, and as it currently stands all clients are
  988. * cleaned up in the removal codepath. In the future this may change
  989. * so that we can support hot-unplugging, but for now we immediately
  990. * return to avoid a double-free situation.
  991. */
  992. if (!drm_dev_enter(dev, &dev_index))
  993. return;
  994. pm_runtime_get_sync(dev->dev);
  995. mutex_lock(&cli->mutex);
  996. if (cli->abi16)
  997. nouveau_abi16_fini(cli->abi16);
  998. mutex_unlock(&cli->mutex);
  999. mutex_lock(&drm->clients_lock);
  1000. list_del(&cli->head);
  1001. mutex_unlock(&drm->clients_lock);
  1002. nouveau_cli_fini(cli);
  1003. kfree(cli);
  1004. pm_runtime_mark_last_busy(dev->dev);
  1005. pm_runtime_put_autosuspend(dev->dev);
  1006. drm_dev_exit(dev_index);
  1007. }
  1008. static const struct drm_ioctl_desc
  1009. nouveau_ioctls[] = {
  1010. DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_RENDER_ALLOW),
  1011. DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  1012. DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_RENDER_ALLOW),
  1013. DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_RENDER_ALLOW),
  1014. DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_RENDER_ALLOW),
  1015. DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_RENDER_ALLOW),
  1016. DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_RENDER_ALLOW),
  1017. DRM_IOCTL_DEF_DRV(NOUVEAU_SVM_INIT, nouveau_svmm_init, DRM_RENDER_ALLOW),
  1018. DRM_IOCTL_DEF_DRV(NOUVEAU_SVM_BIND, nouveau_svmm_bind, DRM_RENDER_ALLOW),
  1019. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_RENDER_ALLOW),
  1020. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_RENDER_ALLOW),
  1021. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_RENDER_ALLOW),
  1022. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_RENDER_ALLOW),
  1023. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_RENDER_ALLOW),
  1024. };
  1025. long
  1026. nouveau_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1027. {
  1028. struct drm_file *filp = file->private_data;
  1029. struct drm_device *dev = filp->minor->dev;
  1030. long ret;
  1031. ret = pm_runtime_get_sync(dev->dev);
  1032. if (ret < 0 && ret != -EACCES) {
  1033. pm_runtime_put_autosuspend(dev->dev);
  1034. return ret;
  1035. }
  1036. switch (_IOC_NR(cmd) - DRM_COMMAND_BASE) {
  1037. case DRM_NOUVEAU_NVIF:
  1038. ret = usif_ioctl(filp, (void __user *)arg, _IOC_SIZE(cmd));
  1039. break;
  1040. default:
  1041. ret = drm_ioctl(file, cmd, arg);
  1042. break;
  1043. }
  1044. pm_runtime_mark_last_busy(dev->dev);
  1045. pm_runtime_put_autosuspend(dev->dev);
  1046. return ret;
  1047. }
  1048. static const struct file_operations
  1049. nouveau_driver_fops = {
  1050. .owner = THIS_MODULE,
  1051. .open = drm_open,
  1052. .release = drm_release,
  1053. .unlocked_ioctl = nouveau_drm_ioctl,
  1054. .mmap = drm_gem_mmap,
  1055. .poll = drm_poll,
  1056. .read = drm_read,
  1057. #if defined(CONFIG_COMPAT)
  1058. .compat_ioctl = nouveau_compat_ioctl,
  1059. #endif
  1060. .llseek = noop_llseek,
  1061. };
  1062. static struct drm_driver
  1063. driver_stub = {
  1064. .driver_features =
  1065. DRIVER_GEM | DRIVER_MODESET | DRIVER_RENDER
  1066. #if defined(CONFIG_NOUVEAU_LEGACY_CTX_SUPPORT)
  1067. | DRIVER_KMS_LEGACY_CONTEXT
  1068. #endif
  1069. ,
  1070. .open = nouveau_drm_open,
  1071. .postclose = nouveau_drm_postclose,
  1072. .lastclose = nouveau_vga_lastclose,
  1073. #if defined(CONFIG_DEBUG_FS)
  1074. .debugfs_init = nouveau_drm_debugfs_init,
  1075. #endif
  1076. .ioctls = nouveau_ioctls,
  1077. .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
  1078. .fops = &nouveau_driver_fops,
  1079. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  1080. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  1081. .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
  1082. .gem_prime_mmap = drm_gem_prime_mmap,
  1083. .dumb_create = nouveau_display_dumb_create,
  1084. .dumb_map_offset = drm_gem_ttm_dumb_map_offset,
  1085. .name = DRIVER_NAME,
  1086. .desc = DRIVER_DESC,
  1087. #ifdef GIT_REVISION
  1088. .date = GIT_REVISION,
  1089. #else
  1090. .date = DRIVER_DATE,
  1091. #endif
  1092. .major = DRIVER_MAJOR,
  1093. .minor = DRIVER_MINOR,
  1094. .patchlevel = DRIVER_PATCHLEVEL,
  1095. };
  1096. static struct pci_device_id
  1097. nouveau_drm_pci_table[] = {
  1098. {
  1099. PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
  1100. .class = PCI_BASE_CLASS_DISPLAY << 16,
  1101. .class_mask = 0xff << 16,
  1102. },
  1103. {
  1104. PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
  1105. .class = PCI_BASE_CLASS_DISPLAY << 16,
  1106. .class_mask = 0xff << 16,
  1107. },
  1108. {}
  1109. };
  1110. static void nouveau_display_options(void)
  1111. {
  1112. DRM_DEBUG_DRIVER("Loading Nouveau with parameters:\n");
  1113. DRM_DEBUG_DRIVER("... tv_disable : %d\n", nouveau_tv_disable);
  1114. DRM_DEBUG_DRIVER("... ignorelid : %d\n", nouveau_ignorelid);
  1115. DRM_DEBUG_DRIVER("... duallink : %d\n", nouveau_duallink);
  1116. DRM_DEBUG_DRIVER("... nofbaccel : %d\n", nouveau_nofbaccel);
  1117. DRM_DEBUG_DRIVER("... config : %s\n", nouveau_config);
  1118. DRM_DEBUG_DRIVER("... debug : %s\n", nouveau_debug);
  1119. DRM_DEBUG_DRIVER("... noaccel : %d\n", nouveau_noaccel);
  1120. DRM_DEBUG_DRIVER("... modeset : %d\n", nouveau_modeset);
  1121. DRM_DEBUG_DRIVER("... runpm : %d\n", nouveau_runtime_pm);
  1122. DRM_DEBUG_DRIVER("... vram_pushbuf : %d\n", nouveau_vram_pushbuf);
  1123. DRM_DEBUG_DRIVER("... hdmimhz : %d\n", nouveau_hdmimhz);
  1124. }
  1125. static const struct dev_pm_ops nouveau_pm_ops = {
  1126. .suspend = nouveau_pmops_suspend,
  1127. .resume = nouveau_pmops_resume,
  1128. .freeze = nouveau_pmops_freeze,
  1129. .thaw = nouveau_pmops_thaw,
  1130. .poweroff = nouveau_pmops_freeze,
  1131. .restore = nouveau_pmops_resume,
  1132. .runtime_suspend = nouveau_pmops_runtime_suspend,
  1133. .runtime_resume = nouveau_pmops_runtime_resume,
  1134. .runtime_idle = nouveau_pmops_runtime_idle,
  1135. };
  1136. static struct pci_driver
  1137. nouveau_drm_pci_driver = {
  1138. .name = "nouveau",
  1139. .id_table = nouveau_drm_pci_table,
  1140. .probe = nouveau_drm_probe,
  1141. .remove = nouveau_drm_remove,
  1142. .driver.pm = &nouveau_pm_ops,
  1143. };
  1144. struct drm_device *
  1145. nouveau_platform_device_create(const struct nvkm_device_tegra_func *func,
  1146. struct platform_device *pdev,
  1147. struct nvkm_device **pdevice)
  1148. {
  1149. struct drm_device *drm;
  1150. int err;
  1151. err = nvkm_device_tegra_new(func, pdev, nouveau_config, nouveau_debug,
  1152. true, true, ~0ULL, pdevice);
  1153. if (err)
  1154. goto err_free;
  1155. drm = drm_dev_alloc(&driver_platform, &pdev->dev);
  1156. if (IS_ERR(drm)) {
  1157. err = PTR_ERR(drm);
  1158. goto err_free;
  1159. }
  1160. err = nouveau_drm_device_init(drm);
  1161. if (err)
  1162. goto err_put;
  1163. platform_set_drvdata(pdev, drm);
  1164. return drm;
  1165. err_put:
  1166. drm_dev_put(drm);
  1167. err_free:
  1168. nvkm_device_del(pdevice);
  1169. return ERR_PTR(err);
  1170. }
  1171. static int __init
  1172. nouveau_drm_init(void)
  1173. {
  1174. driver_pci = driver_stub;
  1175. driver_platform = driver_stub;
  1176. nouveau_display_options();
  1177. if (nouveau_modeset == -1) {
  1178. if (drm_firmware_drivers_only())
  1179. nouveau_modeset = 0;
  1180. }
  1181. if (!nouveau_modeset)
  1182. return 0;
  1183. #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
  1184. platform_driver_register(&nouveau_platform_driver);
  1185. #endif
  1186. nouveau_register_dsm_handler();
  1187. nouveau_backlight_ctor();
  1188. #ifdef CONFIG_PCI
  1189. return pci_register_driver(&nouveau_drm_pci_driver);
  1190. #else
  1191. return 0;
  1192. #endif
  1193. }
  1194. static void __exit
  1195. nouveau_drm_exit(void)
  1196. {
  1197. if (!nouveau_modeset)
  1198. return;
  1199. #ifdef CONFIG_PCI
  1200. pci_unregister_driver(&nouveau_drm_pci_driver);
  1201. #endif
  1202. nouveau_backlight_dtor();
  1203. nouveau_unregister_dsm_handler();
  1204. #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
  1205. platform_driver_unregister(&nouveau_platform_driver);
  1206. #endif
  1207. if (IS_ENABLED(CONFIG_DRM_NOUVEAU_SVM))
  1208. mmu_notifier_synchronize();
  1209. }
  1210. module_init(nouveau_drm_init);
  1211. module_exit(nouveau_drm_exit);
  1212. MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
  1213. MODULE_AUTHOR(DRIVER_AUTHOR);
  1214. MODULE_DESCRIPTION(DRIVER_DESC);
  1215. MODULE_LICENSE("GPL and additional rights");