nouveau_drm.c 26 KB

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