nouveau_drm.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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/console.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 "drmP.h"
  30. #include "drm_crtc_helper.h"
  31. #include <core/device.h>
  32. #include <core/gpuobj.h>
  33. #include <core/option.h>
  34. #include "nouveau_drm.h"
  35. #include "nouveau_dma.h"
  36. #include "nouveau_ttm.h"
  37. #include "nouveau_gem.h"
  38. #include "nouveau_agp.h"
  39. #include "nouveau_vga.h"
  40. #include "nouveau_sysfs.h"
  41. #include "nouveau_hwmon.h"
  42. #include "nouveau_acpi.h"
  43. #include "nouveau_bios.h"
  44. #include "nouveau_ioctl.h"
  45. #include "nouveau_abi16.h"
  46. #include "nouveau_fbcon.h"
  47. #include "nouveau_fence.h"
  48. #include "nouveau_debugfs.h"
  49. #include "nouveau_usif.h"
  50. #include "nouveau_connector.h"
  51. MODULE_PARM_DESC(config, "option string to pass to driver core");
  52. static char *nouveau_config;
  53. module_param_named(config, nouveau_config, charp, 0400);
  54. MODULE_PARM_DESC(debug, "debug string to pass to driver core");
  55. static char *nouveau_debug;
  56. module_param_named(debug, nouveau_debug, charp, 0400);
  57. MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
  58. static int nouveau_noaccel = 0;
  59. module_param_named(noaccel, nouveau_noaccel, int, 0400);
  60. MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
  61. "0 = disabled, 1 = enabled, 2 = headless)");
  62. int nouveau_modeset = -1;
  63. module_param_named(modeset, nouveau_modeset, int, 0400);
  64. MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
  65. int nouveau_runtime_pm = -1;
  66. module_param_named(runpm, nouveau_runtime_pm, int, 0400);
  67. static struct drm_driver driver_stub;
  68. static struct drm_driver driver_pci;
  69. static struct drm_driver driver_platform;
  70. static u64
  71. nouveau_pci_name(struct pci_dev *pdev)
  72. {
  73. u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
  74. name |= pdev->bus->number << 16;
  75. name |= PCI_SLOT(pdev->devfn) << 8;
  76. return name | PCI_FUNC(pdev->devfn);
  77. }
  78. static u64
  79. nouveau_platform_name(struct platform_device *platformdev)
  80. {
  81. return platformdev->id;
  82. }
  83. static u64
  84. nouveau_name(struct drm_device *dev)
  85. {
  86. if (dev->pdev)
  87. return nouveau_pci_name(dev->pdev);
  88. else
  89. return nouveau_platform_name(dev->platformdev);
  90. }
  91. static int
  92. nouveau_cli_create(u64 name, const char *sname,
  93. int size, void **pcli)
  94. {
  95. struct nouveau_cli *cli = *pcli = kzalloc(size, GFP_KERNEL);
  96. if (cli) {
  97. int ret = nvif_client_init(NULL, NULL, sname, name,
  98. nouveau_config, nouveau_debug,
  99. &cli->base);
  100. if (ret == 0) {
  101. mutex_init(&cli->mutex);
  102. usif_client_init(cli);
  103. }
  104. return ret;
  105. }
  106. return -ENOMEM;
  107. }
  108. static void
  109. nouveau_cli_destroy(struct nouveau_cli *cli)
  110. {
  111. nouveau_vm_ref(NULL, &nvkm_client(&cli->base)->vm, NULL);
  112. nvif_client_fini(&cli->base);
  113. usif_client_fini(cli);
  114. }
  115. static void
  116. nouveau_accel_fini(struct nouveau_drm *drm)
  117. {
  118. nouveau_channel_del(&drm->channel);
  119. nvif_object_fini(&drm->ntfy);
  120. nouveau_gpuobj_ref(NULL, &drm->notify);
  121. nvif_object_fini(&drm->nvsw);
  122. nouveau_channel_del(&drm->cechan);
  123. nvif_object_fini(&drm->ttm.copy);
  124. if (drm->fence)
  125. nouveau_fence(drm)->dtor(drm);
  126. }
  127. static void
  128. nouveau_accel_init(struct nouveau_drm *drm)
  129. {
  130. struct nvif_device *device = &drm->device;
  131. u32 arg0, arg1;
  132. u32 sclass[16];
  133. int ret, i;
  134. if (nouveau_noaccel)
  135. return;
  136. /* initialise synchronisation routines */
  137. /*XXX: this is crap, but the fence/channel stuff is a little
  138. * backwards in some places. this will be fixed.
  139. */
  140. ret = nvif_object_sclass(&device->base, sclass, ARRAY_SIZE(sclass));
  141. if (ret < 0)
  142. return;
  143. for (ret = -ENOSYS, i = 0; ret && i < ARRAY_SIZE(sclass); i++) {
  144. switch (sclass[i]) {
  145. case NV03_CHANNEL_DMA:
  146. ret = nv04_fence_create(drm);
  147. break;
  148. case NV10_CHANNEL_DMA:
  149. ret = nv10_fence_create(drm);
  150. break;
  151. case NV17_CHANNEL_DMA:
  152. case NV40_CHANNEL_DMA:
  153. ret = nv17_fence_create(drm);
  154. break;
  155. case NV50_CHANNEL_GPFIFO:
  156. ret = nv50_fence_create(drm);
  157. break;
  158. case G82_CHANNEL_GPFIFO:
  159. ret = nv84_fence_create(drm);
  160. break;
  161. case FERMI_CHANNEL_GPFIFO:
  162. case KEPLER_CHANNEL_GPFIFO_A:
  163. ret = nvc0_fence_create(drm);
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. if (ret) {
  170. NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
  171. nouveau_accel_fini(drm);
  172. return;
  173. }
  174. if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
  175. ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
  176. KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE0|
  177. KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE1,
  178. 0, &drm->cechan);
  179. if (ret)
  180. NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
  181. arg0 = KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR;
  182. arg1 = 1;
  183. } else
  184. if (device->info.chipset >= 0xa3 &&
  185. device->info.chipset != 0xaa &&
  186. device->info.chipset != 0xac) {
  187. ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
  188. NvDmaFB, NvDmaTT, &drm->cechan);
  189. if (ret)
  190. NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
  191. arg0 = NvDmaFB;
  192. arg1 = NvDmaTT;
  193. } else {
  194. arg0 = NvDmaFB;
  195. arg1 = NvDmaTT;
  196. }
  197. ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN, arg0, arg1,
  198. &drm->channel);
  199. if (ret) {
  200. NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
  201. nouveau_accel_fini(drm);
  202. return;
  203. }
  204. ret = nvif_object_init(drm->channel->object, NULL, NVDRM_NVSW,
  205. nouveau_abi16_swclass(drm), NULL, 0, &drm->nvsw);
  206. if (ret == 0) {
  207. struct nouveau_software_chan *swch;
  208. ret = RING_SPACE(drm->channel, 2);
  209. if (ret == 0) {
  210. if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
  211. BEGIN_NV04(drm->channel, NvSubSw, 0, 1);
  212. OUT_RING (drm->channel, NVDRM_NVSW);
  213. } else
  214. if (device->info.family < NV_DEVICE_INFO_V0_KEPLER) {
  215. BEGIN_NVC0(drm->channel, FermiSw, 0, 1);
  216. OUT_RING (drm->channel, 0x001f0000);
  217. }
  218. }
  219. swch = (void *)nvkm_object(&drm->nvsw)->parent;
  220. swch->flip = nouveau_flip_complete;
  221. swch->flip_data = drm->channel;
  222. }
  223. if (ret) {
  224. NV_ERROR(drm, "failed to allocate software object, %d\n", ret);
  225. nouveau_accel_fini(drm);
  226. return;
  227. }
  228. if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
  229. ret = nouveau_gpuobj_new(nvkm_object(&drm->device), NULL, 32,
  230. 0, 0, &drm->notify);
  231. if (ret) {
  232. NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
  233. nouveau_accel_fini(drm);
  234. return;
  235. }
  236. ret = nvif_object_init(drm->channel->object, NULL, NvNotify0,
  237. NV_DMA_IN_MEMORY,
  238. &(struct nv_dma_v0) {
  239. .target = NV_DMA_V0_TARGET_VRAM,
  240. .access = NV_DMA_V0_ACCESS_RDWR,
  241. .start = drm->notify->addr,
  242. .limit = drm->notify->addr + 31
  243. }, sizeof(struct nv_dma_v0),
  244. &drm->ntfy);
  245. if (ret) {
  246. nouveau_accel_fini(drm);
  247. return;
  248. }
  249. }
  250. nouveau_bo_move_init(drm);
  251. }
  252. static int nouveau_drm_probe(struct pci_dev *pdev,
  253. const struct pci_device_id *pent)
  254. {
  255. struct nouveau_device *device;
  256. struct apertures_struct *aper;
  257. bool boot = false;
  258. int ret;
  259. /* remove conflicting drivers (vesafb, efifb etc) */
  260. aper = alloc_apertures(3);
  261. if (!aper)
  262. return -ENOMEM;
  263. aper->ranges[0].base = pci_resource_start(pdev, 1);
  264. aper->ranges[0].size = pci_resource_len(pdev, 1);
  265. aper->count = 1;
  266. if (pci_resource_len(pdev, 2)) {
  267. aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
  268. aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
  269. aper->count++;
  270. }
  271. if (pci_resource_len(pdev, 3)) {
  272. aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
  273. aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
  274. aper->count++;
  275. }
  276. #ifdef CONFIG_X86
  277. boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
  278. #endif
  279. if (nouveau_modeset != 2)
  280. remove_conflicting_framebuffers(aper, "nouveaufb", boot);
  281. kfree(aper);
  282. ret = nouveau_device_create(pdev, NOUVEAU_BUS_PCI,
  283. nouveau_pci_name(pdev), pci_name(pdev),
  284. nouveau_config, nouveau_debug, &device);
  285. if (ret)
  286. return ret;
  287. pci_set_master(pdev);
  288. ret = drm_get_pci_dev(pdev, pent, &driver_pci);
  289. if (ret) {
  290. nouveau_object_ref(NULL, (struct nouveau_object **)&device);
  291. return ret;
  292. }
  293. return 0;
  294. }
  295. #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
  296. static void
  297. nouveau_get_hdmi_dev(struct nouveau_drm *drm)
  298. {
  299. struct pci_dev *pdev = drm->dev->pdev;
  300. if (!pdev) {
  301. DRM_INFO("not a PCI device; no HDMI\n");
  302. drm->hdmi_device = NULL;
  303. return;
  304. }
  305. /* subfunction one is a hdmi audio device? */
  306. drm->hdmi_device = pci_get_bus_and_slot((unsigned int)pdev->bus->number,
  307. PCI_DEVFN(PCI_SLOT(pdev->devfn), 1));
  308. if (!drm->hdmi_device) {
  309. NV_DEBUG(drm, "hdmi device not found %d %d %d\n", pdev->bus->number, PCI_SLOT(pdev->devfn), 1);
  310. return;
  311. }
  312. if ((drm->hdmi_device->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO) {
  313. NV_DEBUG(drm, "possible hdmi device not audio %d\n", drm->hdmi_device->class);
  314. pci_dev_put(drm->hdmi_device);
  315. drm->hdmi_device = NULL;
  316. return;
  317. }
  318. }
  319. static int
  320. nouveau_drm_load(struct drm_device *dev, unsigned long flags)
  321. {
  322. struct pci_dev *pdev = dev->pdev;
  323. struct nouveau_drm *drm;
  324. int ret;
  325. ret = nouveau_cli_create(nouveau_name(dev), "DRM", sizeof(*drm),
  326. (void **)&drm);
  327. if (ret)
  328. return ret;
  329. dev->dev_private = drm;
  330. drm->dev = dev;
  331. nvkm_client(&drm->client.base)->debug =
  332. nouveau_dbgopt(nouveau_debug, "DRM");
  333. INIT_LIST_HEAD(&drm->clients);
  334. spin_lock_init(&drm->tile.lock);
  335. nouveau_get_hdmi_dev(drm);
  336. /* make sure AGP controller is in a consistent state before we
  337. * (possibly) execute vbios init tables (see nouveau_agp.h)
  338. */
  339. if (pdev && drm_pci_device_is_agp(dev) && dev->agp) {
  340. const u64 enables = NV_DEVICE_V0_DISABLE_IDENTIFY |
  341. NV_DEVICE_V0_DISABLE_MMIO;
  342. /* dummy device object, doesn't init anything, but allows
  343. * agp code access to registers
  344. */
  345. ret = nvif_device_init(&drm->client.base.base, NULL,
  346. NVDRM_DEVICE, NV_DEVICE,
  347. &(struct nv_device_v0) {
  348. .device = ~0,
  349. .disable = ~enables,
  350. .debug0 = ~0,
  351. }, sizeof(struct nv_device_v0),
  352. &drm->device);
  353. if (ret)
  354. goto fail_device;
  355. nouveau_agp_reset(drm);
  356. nvif_device_fini(&drm->device);
  357. }
  358. ret = nvif_device_init(&drm->client.base.base, NULL, NVDRM_DEVICE,
  359. NV_DEVICE,
  360. &(struct nv_device_v0) {
  361. .device = ~0,
  362. .disable = 0,
  363. .debug0 = 0,
  364. }, sizeof(struct nv_device_v0),
  365. &drm->device);
  366. if (ret)
  367. goto fail_device;
  368. dev->irq_enabled = true;
  369. /* workaround an odd issue on nvc1 by disabling the device's
  370. * nosnoop capability. hopefully won't cause issues until a
  371. * better fix is found - assuming there is one...
  372. */
  373. if (drm->device.info.chipset == 0xc1)
  374. nvif_mask(&drm->device, 0x00088080, 0x00000800, 0x00000000);
  375. nouveau_vga_init(drm);
  376. nouveau_agp_init(drm);
  377. if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
  378. ret = nouveau_vm_new(nvkm_device(&drm->device), 0, (1ULL << 40),
  379. 0x1000, &drm->client.vm);
  380. if (ret)
  381. goto fail_device;
  382. nvkm_client(&drm->client.base)->vm = drm->client.vm;
  383. }
  384. ret = nouveau_ttm_init(drm);
  385. if (ret)
  386. goto fail_ttm;
  387. ret = nouveau_bios_init(dev);
  388. if (ret)
  389. goto fail_bios;
  390. ret = nouveau_display_create(dev);
  391. if (ret)
  392. goto fail_dispctor;
  393. if (dev->mode_config.num_crtc) {
  394. ret = nouveau_display_init(dev);
  395. if (ret)
  396. goto fail_dispinit;
  397. }
  398. nouveau_sysfs_init(dev);
  399. nouveau_hwmon_init(dev);
  400. nouveau_accel_init(drm);
  401. nouveau_fbcon_init(dev);
  402. if (nouveau_runtime_pm != 0) {
  403. pm_runtime_use_autosuspend(dev->dev);
  404. pm_runtime_set_autosuspend_delay(dev->dev, 5000);
  405. pm_runtime_set_active(dev->dev);
  406. pm_runtime_allow(dev->dev);
  407. pm_runtime_mark_last_busy(dev->dev);
  408. pm_runtime_put(dev->dev);
  409. }
  410. return 0;
  411. fail_dispinit:
  412. nouveau_display_destroy(dev);
  413. fail_dispctor:
  414. nouveau_bios_takedown(dev);
  415. fail_bios:
  416. nouveau_ttm_fini(drm);
  417. fail_ttm:
  418. nouveau_agp_fini(drm);
  419. nouveau_vga_fini(drm);
  420. fail_device:
  421. nvif_device_fini(&drm->device);
  422. nouveau_cli_destroy(&drm->client);
  423. return ret;
  424. }
  425. static int
  426. nouveau_drm_unload(struct drm_device *dev)
  427. {
  428. struct nouveau_drm *drm = nouveau_drm(dev);
  429. pm_runtime_get_sync(dev->dev);
  430. nouveau_fbcon_fini(dev);
  431. nouveau_accel_fini(drm);
  432. nouveau_hwmon_fini(dev);
  433. nouveau_sysfs_fini(dev);
  434. if (dev->mode_config.num_crtc)
  435. nouveau_display_fini(dev);
  436. nouveau_display_destroy(dev);
  437. nouveau_bios_takedown(dev);
  438. nouveau_ttm_fini(drm);
  439. nouveau_agp_fini(drm);
  440. nouveau_vga_fini(drm);
  441. nvif_device_fini(&drm->device);
  442. if (drm->hdmi_device)
  443. pci_dev_put(drm->hdmi_device);
  444. nouveau_cli_destroy(&drm->client);
  445. return 0;
  446. }
  447. void
  448. nouveau_drm_device_remove(struct drm_device *dev)
  449. {
  450. struct nouveau_drm *drm = nouveau_drm(dev);
  451. struct nouveau_client *client;
  452. struct nouveau_object *device;
  453. dev->irq_enabled = false;
  454. client = nvkm_client(&drm->client.base);
  455. device = client->device;
  456. drm_put_dev(dev);
  457. nouveau_object_ref(NULL, &device);
  458. nouveau_object_debug();
  459. }
  460. EXPORT_SYMBOL(nouveau_drm_device_remove);
  461. static void
  462. nouveau_drm_remove(struct pci_dev *pdev)
  463. {
  464. struct drm_device *dev = pci_get_drvdata(pdev);
  465. nouveau_drm_device_remove(dev);
  466. }
  467. static int
  468. nouveau_do_suspend(struct drm_device *dev, bool runtime)
  469. {
  470. struct nouveau_drm *drm = nouveau_drm(dev);
  471. struct nouveau_cli *cli;
  472. int ret;
  473. if (dev->mode_config.num_crtc) {
  474. NV_INFO(drm, "suspending console...\n");
  475. nouveau_fbcon_set_suspend(dev, 1);
  476. NV_INFO(drm, "suspending display...\n");
  477. ret = nouveau_display_suspend(dev, runtime);
  478. if (ret)
  479. return ret;
  480. }
  481. NV_INFO(drm, "evicting buffers...\n");
  482. ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
  483. NV_INFO(drm, "waiting for kernel channels to go idle...\n");
  484. if (drm->cechan) {
  485. ret = nouveau_channel_idle(drm->cechan);
  486. if (ret)
  487. goto fail_display;
  488. }
  489. if (drm->channel) {
  490. ret = nouveau_channel_idle(drm->channel);
  491. if (ret)
  492. goto fail_display;
  493. }
  494. NV_INFO(drm, "suspending client object trees...\n");
  495. if (drm->fence && nouveau_fence(drm)->suspend) {
  496. if (!nouveau_fence(drm)->suspend(drm)) {
  497. ret = -ENOMEM;
  498. goto fail_display;
  499. }
  500. }
  501. list_for_each_entry(cli, &drm->clients, head) {
  502. ret = nvif_client_suspend(&cli->base);
  503. if (ret)
  504. goto fail_client;
  505. }
  506. NV_INFO(drm, "suspending kernel object tree...\n");
  507. ret = nvif_client_suspend(&drm->client.base);
  508. if (ret)
  509. goto fail_client;
  510. nouveau_agp_fini(drm);
  511. return 0;
  512. fail_client:
  513. list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
  514. nvif_client_resume(&cli->base);
  515. }
  516. if (drm->fence && nouveau_fence(drm)->resume)
  517. nouveau_fence(drm)->resume(drm);
  518. fail_display:
  519. if (dev->mode_config.num_crtc) {
  520. NV_INFO(drm, "resuming display...\n");
  521. nouveau_display_resume(dev, runtime);
  522. }
  523. return ret;
  524. }
  525. int nouveau_pmops_suspend(struct device *dev)
  526. {
  527. struct pci_dev *pdev = to_pci_dev(dev);
  528. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  529. int ret;
  530. if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
  531. drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
  532. return 0;
  533. ret = nouveau_do_suspend(drm_dev, false);
  534. if (ret)
  535. return ret;
  536. pci_save_state(pdev);
  537. pci_disable_device(pdev);
  538. pci_ignore_hotplug(pdev);
  539. pci_set_power_state(pdev, PCI_D3hot);
  540. return 0;
  541. }
  542. static int
  543. nouveau_do_resume(struct drm_device *dev, bool runtime)
  544. {
  545. struct nouveau_drm *drm = nouveau_drm(dev);
  546. struct nouveau_cli *cli;
  547. NV_INFO(drm, "re-enabling device...\n");
  548. nouveau_agp_reset(drm);
  549. NV_INFO(drm, "resuming kernel object tree...\n");
  550. nvif_client_resume(&drm->client.base);
  551. nouveau_agp_init(drm);
  552. NV_INFO(drm, "resuming client object trees...\n");
  553. if (drm->fence && nouveau_fence(drm)->resume)
  554. nouveau_fence(drm)->resume(drm);
  555. list_for_each_entry(cli, &drm->clients, head) {
  556. nvif_client_resume(&cli->base);
  557. }
  558. nouveau_run_vbios_init(dev);
  559. if (dev->mode_config.num_crtc) {
  560. NV_INFO(drm, "resuming display...\n");
  561. nouveau_display_resume(dev, runtime);
  562. NV_INFO(drm, "resuming console...\n");
  563. nouveau_fbcon_set_suspend(dev, 0);
  564. }
  565. return 0;
  566. }
  567. int nouveau_pmops_resume(struct device *dev)
  568. {
  569. struct pci_dev *pdev = to_pci_dev(dev);
  570. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  571. int ret;
  572. if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
  573. drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
  574. return 0;
  575. pci_set_power_state(pdev, PCI_D0);
  576. pci_restore_state(pdev);
  577. ret = pci_enable_device(pdev);
  578. if (ret)
  579. return ret;
  580. pci_set_master(pdev);
  581. return nouveau_do_resume(drm_dev, false);
  582. }
  583. static int nouveau_pmops_freeze(struct device *dev)
  584. {
  585. struct pci_dev *pdev = to_pci_dev(dev);
  586. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  587. return nouveau_do_suspend(drm_dev, false);
  588. }
  589. static int nouveau_pmops_thaw(struct device *dev)
  590. {
  591. struct pci_dev *pdev = to_pci_dev(dev);
  592. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  593. return nouveau_do_resume(drm_dev, false);
  594. }
  595. static int
  596. nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
  597. {
  598. struct nouveau_drm *drm = nouveau_drm(dev);
  599. struct nouveau_cli *cli;
  600. char name[32], tmpname[TASK_COMM_LEN];
  601. int ret;
  602. /* need to bring up power immediately if opening device */
  603. ret = pm_runtime_get_sync(dev->dev);
  604. if (ret < 0 && ret != -EACCES)
  605. return ret;
  606. get_task_comm(tmpname, current);
  607. snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
  608. ret = nouveau_cli_create(nouveau_name(dev), name, sizeof(*cli),
  609. (void **)&cli);
  610. if (ret)
  611. goto out_suspend;
  612. cli->base.super = false;
  613. if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
  614. ret = nouveau_vm_new(nvkm_device(&drm->device), 0, (1ULL << 40),
  615. 0x1000, &cli->vm);
  616. if (ret) {
  617. nouveau_cli_destroy(cli);
  618. goto out_suspend;
  619. }
  620. nvkm_client(&cli->base)->vm = cli->vm;
  621. }
  622. fpriv->driver_priv = cli;
  623. mutex_lock(&drm->client.mutex);
  624. list_add(&cli->head, &drm->clients);
  625. mutex_unlock(&drm->client.mutex);
  626. out_suspend:
  627. pm_runtime_mark_last_busy(dev->dev);
  628. pm_runtime_put_autosuspend(dev->dev);
  629. return ret;
  630. }
  631. static void
  632. nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
  633. {
  634. struct nouveau_cli *cli = nouveau_cli(fpriv);
  635. struct nouveau_drm *drm = nouveau_drm(dev);
  636. pm_runtime_get_sync(dev->dev);
  637. if (cli->abi16)
  638. nouveau_abi16_fini(cli->abi16);
  639. mutex_lock(&drm->client.mutex);
  640. list_del(&cli->head);
  641. mutex_unlock(&drm->client.mutex);
  642. }
  643. static void
  644. nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
  645. {
  646. struct nouveau_cli *cli = nouveau_cli(fpriv);
  647. nouveau_cli_destroy(cli);
  648. pm_runtime_mark_last_busy(dev->dev);
  649. pm_runtime_put_autosuspend(dev->dev);
  650. }
  651. static const struct drm_ioctl_desc
  652. nouveau_ioctls[] = {
  653. DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  654. DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  655. DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  656. DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  657. DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  658. DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  659. DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  660. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  661. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  662. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  663. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  664. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  665. };
  666. long
  667. nouveau_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  668. {
  669. struct drm_file *filp = file->private_data;
  670. struct drm_device *dev = filp->minor->dev;
  671. long ret;
  672. ret = pm_runtime_get_sync(dev->dev);
  673. if (ret < 0 && ret != -EACCES)
  674. return ret;
  675. switch (_IOC_NR(cmd) - DRM_COMMAND_BASE) {
  676. case DRM_NOUVEAU_NVIF:
  677. ret = usif_ioctl(filp, (void __user *)arg, _IOC_SIZE(cmd));
  678. break;
  679. default:
  680. ret = drm_ioctl(file, cmd, arg);
  681. break;
  682. }
  683. pm_runtime_mark_last_busy(dev->dev);
  684. pm_runtime_put_autosuspend(dev->dev);
  685. return ret;
  686. }
  687. static const struct file_operations
  688. nouveau_driver_fops = {
  689. .owner = THIS_MODULE,
  690. .open = drm_open,
  691. .release = drm_release,
  692. .unlocked_ioctl = nouveau_drm_ioctl,
  693. .mmap = nouveau_ttm_mmap,
  694. .poll = drm_poll,
  695. .read = drm_read,
  696. #if defined(CONFIG_COMPAT)
  697. .compat_ioctl = nouveau_compat_ioctl,
  698. #endif
  699. .llseek = noop_llseek,
  700. };
  701. static struct drm_driver
  702. driver_stub = {
  703. .driver_features =
  704. DRIVER_USE_AGP |
  705. DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER,
  706. .load = nouveau_drm_load,
  707. .unload = nouveau_drm_unload,
  708. .open = nouveau_drm_open,
  709. .preclose = nouveau_drm_preclose,
  710. .postclose = nouveau_drm_postclose,
  711. .lastclose = nouveau_vga_lastclose,
  712. #if defined(CONFIG_DEBUG_FS)
  713. .debugfs_init = nouveau_debugfs_init,
  714. .debugfs_cleanup = nouveau_debugfs_takedown,
  715. #endif
  716. .get_vblank_counter = drm_vblank_count,
  717. .enable_vblank = nouveau_display_vblank_enable,
  718. .disable_vblank = nouveau_display_vblank_disable,
  719. .get_scanout_position = nouveau_display_scanoutpos,
  720. .get_vblank_timestamp = nouveau_display_vblstamp,
  721. .ioctls = nouveau_ioctls,
  722. .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
  723. .fops = &nouveau_driver_fops,
  724. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  725. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  726. .gem_prime_export = drm_gem_prime_export,
  727. .gem_prime_import = drm_gem_prime_import,
  728. .gem_prime_pin = nouveau_gem_prime_pin,
  729. .gem_prime_res_obj = nouveau_gem_prime_res_obj,
  730. .gem_prime_unpin = nouveau_gem_prime_unpin,
  731. .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
  732. .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
  733. .gem_prime_vmap = nouveau_gem_prime_vmap,
  734. .gem_prime_vunmap = nouveau_gem_prime_vunmap,
  735. .gem_free_object = nouveau_gem_object_del,
  736. .gem_open_object = nouveau_gem_object_open,
  737. .gem_close_object = nouveau_gem_object_close,
  738. .dumb_create = nouveau_display_dumb_create,
  739. .dumb_map_offset = nouveau_display_dumb_map_offset,
  740. .dumb_destroy = drm_gem_dumb_destroy,
  741. .name = DRIVER_NAME,
  742. .desc = DRIVER_DESC,
  743. #ifdef GIT_REVISION
  744. .date = GIT_REVISION,
  745. #else
  746. .date = DRIVER_DATE,
  747. #endif
  748. .major = DRIVER_MAJOR,
  749. .minor = DRIVER_MINOR,
  750. .patchlevel = DRIVER_PATCHLEVEL,
  751. };
  752. static struct pci_device_id
  753. nouveau_drm_pci_table[] = {
  754. {
  755. PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
  756. .class = PCI_BASE_CLASS_DISPLAY << 16,
  757. .class_mask = 0xff << 16,
  758. },
  759. {
  760. PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
  761. .class = PCI_BASE_CLASS_DISPLAY << 16,
  762. .class_mask = 0xff << 16,
  763. },
  764. {}
  765. };
  766. static int nouveau_pmops_runtime_suspend(struct device *dev)
  767. {
  768. struct pci_dev *pdev = to_pci_dev(dev);
  769. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  770. int ret;
  771. if (nouveau_runtime_pm == 0) {
  772. pm_runtime_forbid(dev);
  773. return -EBUSY;
  774. }
  775. /* are we optimus enabled? */
  776. if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
  777. DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
  778. pm_runtime_forbid(dev);
  779. return -EBUSY;
  780. }
  781. nv_debug_level(SILENT);
  782. drm_kms_helper_poll_disable(drm_dev);
  783. vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
  784. nouveau_switcheroo_optimus_dsm();
  785. ret = nouveau_do_suspend(drm_dev, true);
  786. pci_save_state(pdev);
  787. pci_disable_device(pdev);
  788. pci_set_power_state(pdev, PCI_D3cold);
  789. drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
  790. return ret;
  791. }
  792. static int nouveau_pmops_runtime_resume(struct device *dev)
  793. {
  794. struct pci_dev *pdev = to_pci_dev(dev);
  795. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  796. struct nvif_device *device = &nouveau_drm(drm_dev)->device;
  797. int ret;
  798. if (nouveau_runtime_pm == 0)
  799. return -EINVAL;
  800. pci_set_power_state(pdev, PCI_D0);
  801. pci_restore_state(pdev);
  802. ret = pci_enable_device(pdev);
  803. if (ret)
  804. return ret;
  805. pci_set_master(pdev);
  806. ret = nouveau_do_resume(drm_dev, true);
  807. drm_kms_helper_poll_enable(drm_dev);
  808. /* do magic */
  809. nvif_mask(device, 0x88488, (1 << 25), (1 << 25));
  810. vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
  811. drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
  812. nv_debug_level(NORMAL);
  813. return ret;
  814. }
  815. static int nouveau_pmops_runtime_idle(struct device *dev)
  816. {
  817. struct pci_dev *pdev = to_pci_dev(dev);
  818. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  819. struct nouveau_drm *drm = nouveau_drm(drm_dev);
  820. struct drm_crtc *crtc;
  821. if (nouveau_runtime_pm == 0) {
  822. pm_runtime_forbid(dev);
  823. return -EBUSY;
  824. }
  825. /* are we optimus enabled? */
  826. if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
  827. DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
  828. pm_runtime_forbid(dev);
  829. return -EBUSY;
  830. }
  831. /* if we have a hdmi audio device - make sure it has a driver loaded */
  832. if (drm->hdmi_device) {
  833. if (!drm->hdmi_device->driver) {
  834. DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
  835. pm_runtime_mark_last_busy(dev);
  836. return -EBUSY;
  837. }
  838. }
  839. list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
  840. if (crtc->enabled) {
  841. DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
  842. return -EBUSY;
  843. }
  844. }
  845. pm_runtime_mark_last_busy(dev);
  846. pm_runtime_autosuspend(dev);
  847. /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
  848. return 1;
  849. }
  850. static void nouveau_display_options(void)
  851. {
  852. DRM_DEBUG_DRIVER("Loading Nouveau with parameters:\n");
  853. DRM_DEBUG_DRIVER("... tv_disable : %d\n", nouveau_tv_disable);
  854. DRM_DEBUG_DRIVER("... ignorelid : %d\n", nouveau_ignorelid);
  855. DRM_DEBUG_DRIVER("... duallink : %d\n", nouveau_duallink);
  856. DRM_DEBUG_DRIVER("... nofbaccel : %d\n", nouveau_nofbaccel);
  857. DRM_DEBUG_DRIVER("... config : %s\n", nouveau_config);
  858. DRM_DEBUG_DRIVER("... debug : %s\n", nouveau_debug);
  859. DRM_DEBUG_DRIVER("... noaccel : %d\n", nouveau_noaccel);
  860. DRM_DEBUG_DRIVER("... modeset : %d\n", nouveau_modeset);
  861. DRM_DEBUG_DRIVER("... runpm : %d\n", nouveau_runtime_pm);
  862. DRM_DEBUG_DRIVER("... vram_pushbuf : %d\n", nouveau_vram_pushbuf);
  863. DRM_DEBUG_DRIVER("... pstate : %d\n", nouveau_pstate);
  864. }
  865. static const struct dev_pm_ops nouveau_pm_ops = {
  866. .suspend = nouveau_pmops_suspend,
  867. .resume = nouveau_pmops_resume,
  868. .freeze = nouveau_pmops_freeze,
  869. .thaw = nouveau_pmops_thaw,
  870. .poweroff = nouveau_pmops_freeze,
  871. .restore = nouveau_pmops_resume,
  872. .runtime_suspend = nouveau_pmops_runtime_suspend,
  873. .runtime_resume = nouveau_pmops_runtime_resume,
  874. .runtime_idle = nouveau_pmops_runtime_idle,
  875. };
  876. static struct pci_driver
  877. nouveau_drm_pci_driver = {
  878. .name = "nouveau",
  879. .id_table = nouveau_drm_pci_table,
  880. .probe = nouveau_drm_probe,
  881. .remove = nouveau_drm_remove,
  882. .driver.pm = &nouveau_pm_ops,
  883. };
  884. struct drm_device *
  885. nouveau_platform_device_create_(struct platform_device *pdev, int size,
  886. void **pobject)
  887. {
  888. struct drm_device *drm;
  889. int err;
  890. err = nouveau_device_create_(pdev, NOUVEAU_BUS_PLATFORM,
  891. nouveau_platform_name(pdev),
  892. dev_name(&pdev->dev), nouveau_config,
  893. nouveau_debug, size, pobject);
  894. if (err)
  895. return ERR_PTR(err);
  896. drm = drm_dev_alloc(&driver_platform, &pdev->dev);
  897. if (!drm) {
  898. err = -ENOMEM;
  899. goto err_free;
  900. }
  901. err = drm_dev_set_unique(drm, "%s", dev_name(&pdev->dev));
  902. if (err < 0)
  903. goto err_free;
  904. drm->platformdev = pdev;
  905. platform_set_drvdata(pdev, drm);
  906. return drm;
  907. err_free:
  908. nouveau_object_ref(NULL, (struct nouveau_object **)pobject);
  909. return ERR_PTR(err);
  910. }
  911. EXPORT_SYMBOL(nouveau_platform_device_create_);
  912. static int __init
  913. nouveau_drm_init(void)
  914. {
  915. driver_pci = driver_stub;
  916. driver_pci.set_busid = drm_pci_set_busid;
  917. driver_platform = driver_stub;
  918. driver_platform.set_busid = drm_platform_set_busid;
  919. nouveau_display_options();
  920. if (nouveau_modeset == -1) {
  921. #ifdef CONFIG_VGA_CONSOLE
  922. if (vgacon_text_force())
  923. nouveau_modeset = 0;
  924. #endif
  925. }
  926. if (!nouveau_modeset)
  927. return 0;
  928. nouveau_register_dsm_handler();
  929. return drm_pci_init(&driver_pci, &nouveau_drm_pci_driver);
  930. }
  931. static void __exit
  932. nouveau_drm_exit(void)
  933. {
  934. if (!nouveau_modeset)
  935. return;
  936. drm_pci_exit(&driver_pci, &nouveau_drm_pci_driver);
  937. nouveau_unregister_dsm_handler();
  938. }
  939. module_init(nouveau_drm_init);
  940. module_exit(nouveau_drm_exit);
  941. MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
  942. MODULE_AUTHOR(DRIVER_AUTHOR);
  943. MODULE_DESCRIPTION(DRIVER_DESC);
  944. MODULE_LICENSE("GPL and additional rights");