Commit | Line | Data |
---|---|---|
94580299 BS |
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 | ||
25 | #include <linux/module.h> | |
26 | #include <linux/pci.h> | |
27 | ||
28 | #include <core/device.h> | |
29 | #include <core/client.h> | |
ebb945a9 | 30 | #include <core/gpuobj.h> |
94580299 BS |
31 | #include <core/class.h> |
32 | ||
33 | #include <subdev/device.h> | |
ebb945a9 | 34 | #include <subdev/vm.h> |
94580299 BS |
35 | |
36 | #include "nouveau_drm.h" | |
ebb945a9 | 37 | #include "nouveau_dma.h" |
cb75d97e | 38 | #include "nouveau_agp.h" |
ebb945a9 BS |
39 | #include "nouveau_abi16.h" |
40 | #include "nouveau_fbcon.h" | |
41 | #include "nouveau_fence.h" | |
42 | ||
43 | #include "nouveau_ttm.h" | |
94580299 BS |
44 | |
45 | int __devinit nouveau_pci_probe(struct pci_dev *, const struct pci_device_id *); | |
46 | void nouveau_pci_remove(struct pci_dev *); | |
47 | int nouveau_pci_suspend(struct pci_dev *, pm_message_t); | |
48 | int nouveau_pci_resume(struct pci_dev *); | |
49 | int __init nouveau_init(struct pci_driver *); | |
50 | void __exit nouveau_exit(struct pci_driver *); | |
51 | ||
52 | int nouveau_load(struct drm_device *, unsigned long); | |
53 | int nouveau_unload(struct drm_device *); | |
94580299 BS |
54 | |
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 | ||
59 | MODULE_PARM_DESC(debug, "debug string to pass to driver core"); | |
60 | static char *nouveau_debug; | |
61 | module_param_named(debug, nouveau_debug, charp, 0400); | |
62 | ||
ebb945a9 BS |
63 | MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration"); |
64 | static int nouveau_noaccel = 0; | |
65 | module_param_named(noaccel, nouveau_noaccel, int, 0400); | |
66 | ||
94580299 BS |
67 | static u64 |
68 | nouveau_name(struct pci_dev *pdev) | |
69 | { | |
70 | u64 name = (u64)pci_domain_nr(pdev->bus) << 32; | |
71 | name |= pdev->bus->number << 16; | |
72 | name |= PCI_SLOT(pdev->devfn) << 8; | |
73 | return name | PCI_FUNC(pdev->devfn); | |
74 | } | |
75 | ||
76 | static int | |
77 | nouveau_cli_create(struct pci_dev *pdev, u32 name, int size, void **pcli) | |
78 | { | |
79 | struct nouveau_cli *cli; | |
80 | int ret; | |
81 | ||
82 | ret = nouveau_client_create_(name, nouveau_name(pdev), nouveau_config, | |
83 | nouveau_debug, size, pcli); | |
84 | cli = *pcli; | |
85 | if (ret) | |
86 | return ret; | |
87 | ||
88 | mutex_init(&cli->mutex); | |
89 | return 0; | |
90 | } | |
91 | ||
92 | static void | |
93 | nouveau_cli_destroy(struct nouveau_cli *cli) | |
94 | { | |
95 | struct nouveau_object *client = nv_object(cli); | |
ebb945a9 | 96 | nouveau_vm_ref(NULL, &cli->base.vm, NULL); |
94580299 BS |
97 | nouveau_client_fini(&cli->base, false); |
98 | atomic_set(&client->refcount, 1); | |
99 | nouveau_object_ref(NULL, &client); | |
100 | } | |
101 | ||
ebb945a9 BS |
102 | static void |
103 | nouveau_accel_fini(struct nouveau_drm *drm) | |
104 | { | |
105 | nouveau_gpuobj_ref(NULL, &drm->notify); | |
106 | nouveau_channel_del(&drm->channel); | |
107 | if (drm->fence) | |
108 | nouveau_fence(drm)->dtor(drm); | |
109 | } | |
110 | ||
111 | static void | |
112 | nouveau_accel_init(struct nouveau_drm *drm) | |
113 | { | |
114 | struct nouveau_device *device = nv_device(drm->device); | |
115 | struct nouveau_object *object; | |
116 | int ret; | |
117 | ||
118 | if (nouveau_noaccel) | |
119 | return; | |
120 | ||
121 | /* initialise synchronisation routines */ | |
122 | if (device->card_type < NV_10) ret = nv04_fence_create(drm); | |
123 | else if (device->chipset < 0x84) ret = nv10_fence_create(drm); | |
124 | else if (device->card_type < NV_C0) ret = nv84_fence_create(drm); | |
125 | else ret = nvc0_fence_create(drm); | |
126 | if (ret) { | |
127 | NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret); | |
128 | nouveau_accel_fini(drm); | |
129 | return; | |
130 | } | |
131 | ||
132 | ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE, NVDRM_CHAN, | |
133 | NvDmaFB, NvDmaTT, &drm->channel); | |
134 | if (ret) { | |
135 | NV_ERROR(drm, "failed to create kernel channel, %d\n", ret); | |
136 | nouveau_accel_fini(drm); | |
137 | return; | |
138 | } | |
139 | ||
140 | if (device->card_type < NV_C0) { | |
141 | ret = nouveau_gpuobj_new(drm->device, NULL, 32, 0, 0, | |
142 | &drm->notify); | |
143 | if (ret) { | |
144 | NV_ERROR(drm, "failed to allocate notifier, %d\n", ret); | |
145 | nouveau_accel_fini(drm); | |
146 | return; | |
147 | } | |
148 | ||
149 | ret = nouveau_object_new(nv_object(drm), | |
150 | drm->channel->handle, NvNotify0, | |
151 | 0x003d, &(struct nv_dma_class) { | |
152 | .flags = NV_DMA_TARGET_VRAM | | |
153 | NV_DMA_ACCESS_RDWR, | |
154 | .start = drm->notify->addr, | |
155 | .limit = drm->notify->addr + 31 | |
156 | }, sizeof(struct nv_dma_class), | |
157 | &object); | |
158 | if (ret) { | |
159 | nouveau_accel_fini(drm); | |
160 | return; | |
161 | } | |
162 | } | |
163 | ||
164 | ||
165 | nouveau_bo_move_init(drm->channel); | |
166 | } | |
167 | ||
94580299 BS |
168 | static int __devinit |
169 | nouveau_drm_probe(struct pci_dev *pdev, const struct pci_device_id *pent) | |
170 | { | |
171 | struct nouveau_device *device; | |
ebb945a9 BS |
172 | struct apertures_struct *aper; |
173 | bool boot = false; | |
94580299 BS |
174 | int ret; |
175 | ||
ebb945a9 BS |
176 | /* remove conflicting drivers (vesafb, efifb etc) */ |
177 | aper = alloc_apertures(3); | |
178 | if (!aper) | |
179 | return -ENOMEM; | |
180 | ||
181 | aper->ranges[0].base = pci_resource_start(pdev, 1); | |
182 | aper->ranges[0].size = pci_resource_len(pdev, 1); | |
183 | aper->count = 1; | |
184 | ||
185 | if (pci_resource_len(pdev, 2)) { | |
186 | aper->ranges[aper->count].base = pci_resource_start(pdev, 2); | |
187 | aper->ranges[aper->count].size = pci_resource_len(pdev, 2); | |
188 | aper->count++; | |
189 | } | |
190 | ||
191 | if (pci_resource_len(pdev, 3)) { | |
192 | aper->ranges[aper->count].base = pci_resource_start(pdev, 3); | |
193 | aper->ranges[aper->count].size = pci_resource_len(pdev, 3); | |
194 | aper->count++; | |
195 | } | |
196 | ||
197 | #ifdef CONFIG_X86 | |
198 | boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW; | |
199 | #endif | |
200 | remove_conflicting_framebuffers(aper, "nouveaufb", boot); | |
201 | ||
94580299 BS |
202 | ret = nouveau_device_create(pdev, nouveau_name(pdev), pci_name(pdev), |
203 | nouveau_config, nouveau_debug, &device); | |
204 | if (ret) | |
205 | return ret; | |
206 | ||
207 | pci_set_master(pdev); | |
208 | ||
209 | ret = nouveau_pci_probe(pdev, pent); | |
210 | if (ret) { | |
ebb945a9 | 211 | nouveau_object_ref(NULL, (struct nouveau_object **)&device); |
94580299 BS |
212 | return ret; |
213 | } | |
214 | ||
215 | return 0; | |
216 | } | |
217 | ||
218 | int | |
219 | nouveau_drm_load(struct drm_device *dev, unsigned long flags) | |
220 | { | |
221 | struct pci_dev *pdev = dev->pdev; | |
ebb945a9 | 222 | struct nouveau_device *device; |
94580299 BS |
223 | struct nouveau_drm *drm; |
224 | int ret; | |
225 | ||
226 | ret = nouveau_cli_create(pdev, 0, sizeof(*drm), (void**)&drm); | |
227 | dev->dev_private = drm; | |
228 | if (ret) | |
229 | return ret; | |
230 | ||
231 | INIT_LIST_HEAD(&drm->clients); | |
ebb945a9 | 232 | spin_lock_init(&drm->tile.lock); |
94580299 BS |
233 | drm->dev = dev; |
234 | ||
cb75d97e BS |
235 | /* make sure AGP controller is in a consistent state before we |
236 | * (possibly) execute vbios init tables (see nouveau_agp.h) | |
237 | */ | |
238 | if (drm_pci_device_is_agp(dev) && dev->agp) { | |
239 | /* dummy device object, doesn't init anything, but allows | |
240 | * agp code access to registers | |
241 | */ | |
242 | ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT, | |
243 | NVDRM_DEVICE, 0x0080, | |
244 | &(struct nv_device_class) { | |
245 | .device = ~0, | |
246 | .disable = | |
247 | ~(NV_DEVICE_DISABLE_MMIO | | |
248 | NV_DEVICE_DISABLE_IDENTIFY), | |
249 | .debug0 = ~0, | |
250 | }, sizeof(struct nv_device_class), | |
251 | &drm->device); | |
252 | if (ret) | |
ebb945a9 | 253 | goto fail_device; |
cb75d97e BS |
254 | |
255 | nouveau_agp_reset(drm); | |
256 | nouveau_object_del(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE); | |
257 | } | |
258 | ||
94580299 BS |
259 | ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE, |
260 | 0x0080, &(struct nv_device_class) { | |
261 | .device = ~0, | |
262 | .disable = 0, | |
263 | .debug0 = 0, | |
264 | }, sizeof(struct nv_device_class), | |
265 | &drm->device); | |
266 | if (ret) | |
267 | goto fail_device; | |
268 | ||
ebb945a9 BS |
269 | device = nv_device(drm->device); |
270 | ||
cb75d97e BS |
271 | /* initialise AGP */ |
272 | nouveau_agp_init(drm); | |
273 | ||
ebb945a9 BS |
274 | if (device->card_type >= NV_50) { |
275 | ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40), | |
276 | 0x1000, &drm->client.base.vm); | |
277 | if (ret) | |
278 | goto fail_device; | |
279 | } | |
280 | ||
281 | ret = nouveau_ttm_init(drm); | |
94580299 BS |
282 | if (ret) |
283 | goto fail_device; | |
284 | ||
ebb945a9 BS |
285 | ret = nouveau_load(dev, flags); |
286 | if (ret) | |
287 | goto fail_load; | |
288 | ||
289 | nouveau_accel_init(drm); | |
290 | nouveau_fbcon_init(dev); | |
94580299 BS |
291 | return 0; |
292 | ||
ebb945a9 BS |
293 | fail_load: |
294 | nouveau_ttm_fini(drm); | |
94580299 BS |
295 | fail_device: |
296 | nouveau_cli_destroy(&drm->client); | |
297 | return ret; | |
298 | } | |
299 | ||
300 | int | |
301 | nouveau_drm_unload(struct drm_device *dev) | |
302 | { | |
303 | struct nouveau_drm *drm = nouveau_newpriv(dev); | |
304 | struct pci_dev *pdev = dev->pdev; | |
305 | int ret; | |
306 | ||
ebb945a9 BS |
307 | nouveau_fbcon_fini(dev); |
308 | nouveau_accel_fini(drm); | |
309 | ||
94580299 BS |
310 | ret = nouveau_unload(dev); |
311 | if (ret) | |
312 | return ret; | |
313 | ||
ebb945a9 | 314 | nouveau_ttm_fini(drm); |
cb75d97e BS |
315 | nouveau_agp_fini(drm); |
316 | ||
94580299 BS |
317 | pci_set_drvdata(pdev, drm->client.base.device); |
318 | nouveau_cli_destroy(&drm->client); | |
319 | return 0; | |
320 | } | |
321 | ||
322 | static void | |
323 | nouveau_drm_remove(struct pci_dev *pdev) | |
324 | { | |
ebb945a9 | 325 | struct nouveau_object *device; |
94580299 BS |
326 | nouveau_pci_remove(pdev); |
327 | device = pci_get_drvdata(pdev); | |
ebb945a9 BS |
328 | nouveau_object_ref(NULL, &device); |
329 | nouveau_object_debug(); | |
94580299 BS |
330 | } |
331 | ||
332 | int | |
333 | nouveau_drm_suspend(struct pci_dev *pdev, pm_message_t pm_state) | |
334 | { | |
335 | struct drm_device *dev = pci_get_drvdata(pdev); | |
336 | struct nouveau_drm *drm = nouveau_newpriv(dev); | |
337 | struct nouveau_cli *cli; | |
338 | int ret; | |
339 | ||
340 | if (dev->switch_power_state == DRM_SWITCH_POWER_OFF || | |
341 | pm_state.event == PM_EVENT_PRETHAW) | |
342 | return 0; | |
343 | ||
ebb945a9 BS |
344 | NV_INFO(drm, "suspending fbcon...\n"); |
345 | nouveau_fbcon_set_suspend(dev, 1); | |
346 | ||
347 | NV_INFO(drm, "suspending drm...\n"); | |
94580299 BS |
348 | ret = nouveau_pci_suspend(pdev, pm_state); |
349 | if (ret) | |
350 | return ret; | |
351 | ||
ebb945a9 BS |
352 | NV_INFO(drm, "evicting buffers...\n"); |
353 | ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM); | |
354 | ||
355 | if (drm->fence && nouveau_fence(drm)->suspend) { | |
356 | if (!nouveau_fence(drm)->suspend(drm)) | |
357 | return -ENOMEM; | |
358 | } | |
359 | ||
360 | NV_INFO(drm, "suspending client object trees...\n"); | |
94580299 BS |
361 | list_for_each_entry(cli, &drm->clients, head) { |
362 | ret = nouveau_client_fini(&cli->base, true); | |
363 | if (ret) | |
364 | goto fail_client; | |
365 | } | |
366 | ||
367 | ret = nouveau_client_fini(&drm->client.base, true); | |
368 | if (ret) | |
369 | goto fail_client; | |
370 | ||
cb75d97e BS |
371 | nouveau_agp_fini(drm); |
372 | ||
94580299 BS |
373 | pci_save_state(pdev); |
374 | if (pm_state.event == PM_EVENT_SUSPEND) { | |
375 | pci_disable_device(pdev); | |
376 | pci_set_power_state(pdev, PCI_D3hot); | |
377 | } | |
378 | ||
379 | return 0; | |
380 | ||
381 | fail_client: | |
382 | list_for_each_entry_continue_reverse(cli, &drm->clients, head) { | |
383 | nouveau_client_init(&cli->base); | |
384 | } | |
385 | ||
386 | nouveau_pci_resume(pdev); | |
387 | return ret; | |
388 | } | |
389 | ||
390 | int | |
391 | nouveau_drm_resume(struct pci_dev *pdev) | |
392 | { | |
393 | struct drm_device *dev = pci_get_drvdata(pdev); | |
394 | struct nouveau_drm *drm = nouveau_newpriv(dev); | |
395 | struct nouveau_cli *cli; | |
396 | int ret; | |
397 | ||
398 | if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) | |
399 | return 0; | |
400 | ||
ebb945a9 | 401 | NV_INFO(drm, "re-enabling device...\n"); |
94580299 BS |
402 | pci_set_power_state(pdev, PCI_D0); |
403 | pci_restore_state(pdev); | |
404 | ret = pci_enable_device(pdev); | |
405 | if (ret) | |
406 | return ret; | |
407 | pci_set_master(pdev); | |
408 | ||
cb75d97e BS |
409 | nouveau_agp_reset(drm); |
410 | ||
ebb945a9 | 411 | NV_INFO(drm, "resuming client object trees...\n"); |
94580299 | 412 | nouveau_client_init(&drm->client.base); |
ebb945a9 | 413 | nouveau_agp_init(drm); |
94580299 BS |
414 | |
415 | list_for_each_entry(cli, &drm->clients, head) { | |
416 | nouveau_client_init(&cli->base); | |
417 | } | |
cb75d97e | 418 | |
ebb945a9 BS |
419 | if (drm->fence && nouveau_fence(drm)->resume) |
420 | nouveau_fence(drm)->resume(drm); | |
94580299 BS |
421 | |
422 | return nouveau_pci_resume(pdev); | |
423 | } | |
424 | ||
ebb945a9 BS |
425 | int |
426 | nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv) | |
427 | { | |
428 | struct pci_dev *pdev = dev->pdev; | |
429 | struct nouveau_drm *drm = nouveau_drm(dev); | |
430 | struct nouveau_cli *cli; | |
431 | int ret; | |
432 | ||
433 | ret = nouveau_cli_create(pdev, fpriv->pid, sizeof(*cli), (void **)&cli); | |
434 | if (ret) | |
435 | return ret; | |
436 | ||
437 | if (nv_device(drm->device)->card_type >= NV_50) { | |
438 | ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40), | |
439 | 0x1000, &cli->base.vm); | |
440 | if (ret) { | |
441 | nouveau_cli_destroy(cli); | |
442 | return ret; | |
443 | } | |
444 | } | |
445 | ||
446 | fpriv->driver_priv = cli; | |
447 | ||
448 | mutex_lock(&drm->client.mutex); | |
449 | list_add(&cli->head, &drm->clients); | |
450 | mutex_unlock(&drm->client.mutex); | |
451 | return 0; | |
452 | } | |
453 | ||
454 | void | |
455 | nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv) | |
456 | { | |
457 | struct nouveau_cli *cli = nouveau_cli(fpriv); | |
458 | struct nouveau_drm *drm = nouveau_drm(dev); | |
459 | ||
460 | if (cli->abi16) | |
461 | nouveau_abi16_fini(cli->abi16); | |
462 | ||
463 | mutex_lock(&drm->client.mutex); | |
464 | list_del(&cli->head); | |
465 | mutex_unlock(&drm->client.mutex); | |
466 | } | |
467 | ||
468 | void | |
469 | nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv) | |
470 | { | |
471 | struct nouveau_cli *cli = nouveau_cli(fpriv); | |
472 | nouveau_cli_destroy(cli); | |
473 | } | |
474 | ||
94580299 BS |
475 | static struct pci_device_id |
476 | nouveau_drm_pci_table[] = { | |
477 | { | |
478 | PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID), | |
479 | .class = PCI_BASE_CLASS_DISPLAY << 16, | |
480 | .class_mask = 0xff << 16, | |
481 | }, | |
482 | { | |
483 | PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID), | |
484 | .class = PCI_BASE_CLASS_DISPLAY << 16, | |
485 | .class_mask = 0xff << 16, | |
486 | }, | |
487 | {} | |
488 | }; | |
489 | ||
490 | static struct pci_driver | |
491 | nouveau_drm_pci_driver = { | |
492 | .name = "nouveau", | |
493 | .id_table = nouveau_drm_pci_table, | |
494 | .probe = nouveau_drm_probe, | |
495 | .remove = nouveau_drm_remove, | |
496 | .suspend = nouveau_drm_suspend, | |
497 | .resume = nouveau_drm_resume, | |
498 | }; | |
499 | ||
500 | static int __init | |
501 | nouveau_drm_init(void) | |
502 | { | |
503 | return nouveau_init(&nouveau_drm_pci_driver); | |
504 | } | |
505 | ||
506 | static void __exit | |
507 | nouveau_drm_exit(void) | |
508 | { | |
509 | nouveau_exit(&nouveau_drm_pci_driver); | |
510 | } | |
511 | ||
512 | module_init(nouveau_drm_init); | |
513 | module_exit(nouveau_drm_exit); | |
514 | ||
515 | MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table); | |
516 | MODULE_AUTHOR("Nouveau Project"); | |
517 | MODULE_DESCRIPTION("nVidia Riva/TNT/GeForce/Quadro/Tesla"); | |
518 | MODULE_LICENSE("GPL and additional rights"); |