Home | History | Annotate | Line # | Download | only in nouveau
      1  1.23  riastrad /*	$NetBSD: nouveau_drm.c,v 1.23 2021/12/19 11:34:44 riastradh Exp $	*/
      2   1.2  riastrad 
      3   1.1  riastrad /*
      4   1.1  riastrad  * Copyright 2012 Red Hat Inc.
      5   1.1  riastrad  *
      6   1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7   1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8   1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9   1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10   1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11   1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12   1.1  riastrad  *
     13   1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     14   1.1  riastrad  * all copies or substantial portions of the Software.
     15   1.1  riastrad  *
     16   1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17   1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18   1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19   1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20   1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21   1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22   1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     23   1.1  riastrad  *
     24   1.1  riastrad  * Authors: Ben Skeggs
     25   1.1  riastrad  */
     26   1.1  riastrad 
     27   1.2  riastrad #include <sys/cdefs.h>
     28  1.23  riastrad __KERNEL_RCSID(0, "$NetBSD: nouveau_drm.c,v 1.23 2021/12/19 11:34:44 riastradh Exp $");
     29   1.2  riastrad 
     30   1.1  riastrad #include <linux/console.h>
     31   1.9  riastrad #include <linux/delay.h>
     32   1.1  riastrad #include <linux/module.h>
     33   1.1  riastrad #include <linux/pci.h>
     34   1.1  riastrad #include <linux/pm_runtime.h>
     35   1.1  riastrad #include <linux/vga_switcheroo.h>
     36  1.20  riastrad #include <linux/mmu_notifier.h>
     37   1.9  riastrad 
     38  1.20  riastrad #include <drm/drm_crtc_helper.h>
     39  1.20  riastrad #include <drm/drm_ioctl.h>
     40  1.20  riastrad #include <drm/drm_vblank.h>
     41   1.9  riastrad 
     42   1.1  riastrad #include <core/gpuobj.h>
     43   1.1  riastrad #include <core/option.h>
     44   1.9  riastrad #include <core/pci.h>
     45   1.9  riastrad #include <core/tegra.h>
     46   1.1  riastrad 
     47  1.20  riastrad #include <nvif/driver.h>
     48  1.20  riastrad #include <nvif/fifo.h>
     49  1.20  riastrad #include <nvif/user.h>
     50  1.20  riastrad 
     51  1.20  riastrad #include <nvif/class.h>
     52  1.20  riastrad #include <nvif/cl0002.h>
     53  1.20  riastrad #include <nvif/cla06f.h>
     54  1.20  riastrad 
     55  1.20  riastrad #include "nouveau_drv.h"
     56   1.1  riastrad #include "nouveau_dma.h"
     57   1.1  riastrad #include "nouveau_ttm.h"
     58   1.1  riastrad #include "nouveau_gem.h"
     59   1.1  riastrad #include "nouveau_vga.h"
     60  1.20  riastrad #include "nouveau_led.h"
     61   1.1  riastrad #include "nouveau_hwmon.h"
     62   1.1  riastrad #include "nouveau_acpi.h"
     63   1.1  riastrad #include "nouveau_bios.h"
     64   1.1  riastrad #include "nouveau_ioctl.h"
     65   1.1  riastrad #include "nouveau_abi16.h"
     66   1.1  riastrad #include "nouveau_fbcon.h"
     67   1.1  riastrad #include "nouveau_fence.h"
     68   1.1  riastrad #include "nouveau_debugfs.h"
     69   1.9  riastrad #include "nouveau_usif.h"
     70   1.9  riastrad #include "nouveau_connector.h"
     71   1.9  riastrad #include "nouveau_platform.h"
     72  1.20  riastrad #include "nouveau_svm.h"
     73  1.20  riastrad #include "nouveau_dmem.h"
     74   1.1  riastrad 
     75  1.19  riastrad #ifdef __NetBSD__
     76  1.19  riastrad #include <sys/file.h>
     77  1.19  riastrad #include <sys/ioccom.h>
     78  1.18  riastrad #include <linux/nbsd-namespace.h>
     79  1.19  riastrad #endif
     80  1.18  riastrad 
     81   1.1  riastrad MODULE_PARM_DESC(config, "option string to pass to driver core");
     82   1.5  riastrad char *nouveau_config;
     83   1.1  riastrad module_param_named(config, nouveau_config, charp, 0400);
     84   1.1  riastrad 
     85   1.1  riastrad MODULE_PARM_DESC(debug, "debug string to pass to driver core");
     86   1.5  riastrad char *nouveau_debug;
     87   1.1  riastrad module_param_named(debug, nouveau_debug, charp, 0400);
     88   1.1  riastrad 
     89   1.1  riastrad MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
     90   1.1  riastrad static int nouveau_noaccel = 0;
     91   1.1  riastrad module_param_named(noaccel, nouveau_noaccel, int, 0400);
     92   1.1  riastrad 
     93   1.1  riastrad MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
     94   1.1  riastrad 		          "0 = disabled, 1 = enabled, 2 = headless)");
     95   1.1  riastrad int nouveau_modeset = -1;
     96   1.1  riastrad module_param_named(modeset, nouveau_modeset, int, 0400);
     97   1.1  riastrad 
     98  1.20  riastrad MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
     99  1.20  riastrad static int nouveau_atomic = 0;
    100  1.20  riastrad module_param_named(atomic, nouveau_atomic, int, 0400);
    101  1.20  riastrad 
    102   1.1  riastrad MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
    103  1.20  riastrad static int nouveau_runtime_pm = -1;
    104   1.1  riastrad module_param_named(runpm, nouveau_runtime_pm, int, 0400);
    105   1.1  riastrad 
    106   1.9  riastrad static struct drm_driver driver_stub;
    107   1.9  riastrad static struct drm_driver driver_pci;
    108   1.9  riastrad static struct drm_driver driver_platform;
    109   1.9  riastrad 
    110   1.3  riastrad #ifdef __NetBSD__
    111   1.9  riastrad struct drm_driver *const nouveau_drm_driver_stub = &driver_stub;
    112   1.9  riastrad struct drm_driver *const nouveau_drm_driver_pci = &driver_pci;
    113   1.9  riastrad struct drm_driver *const nouveau_drm_driver_platform = &driver_platform;
    114   1.6       mrg 
    115   1.6       mrg /* XXX Kludge for the non-GEM GEM that nouveau uses.  */
    116   1.6       mrg static const struct uvm_pagerops nouveau_gem_uvm_ops;
    117   1.3  riastrad #endif
    118   1.1  riastrad 
    119   1.1  riastrad static u64
    120   1.1  riastrad nouveau_pci_name(struct pci_dev *pdev)
    121   1.1  riastrad {
    122   1.1  riastrad 	u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
    123   1.1  riastrad 	name |= pdev->bus->number << 16;
    124   1.1  riastrad 	name |= PCI_SLOT(pdev->devfn) << 8;
    125   1.1  riastrad 	return name | PCI_FUNC(pdev->devfn);
    126   1.1  riastrad }
    127   1.1  riastrad 
    128   1.1  riastrad static u64
    129   1.1  riastrad nouveau_platform_name(struct platform_device *platformdev)
    130   1.1  riastrad {
    131   1.1  riastrad 	return platformdev->id;
    132   1.1  riastrad }
    133   1.1  riastrad 
    134   1.1  riastrad static u64
    135   1.1  riastrad nouveau_name(struct drm_device *dev)
    136   1.1  riastrad {
    137   1.1  riastrad 	if (dev->pdev)
    138   1.1  riastrad 		return nouveau_pci_name(dev->pdev);
    139   1.1  riastrad 	else
    140  1.20  riastrad 		return nouveau_platform_name(to_platform_device(dev->dev));
    141   1.1  riastrad }
    142   1.1  riastrad 
    143  1.20  riastrad static inline bool
    144  1.20  riastrad nouveau_cli_work_ready(struct dma_fence *fence)
    145   1.1  riastrad {
    146  1.20  riastrad 	if (!dma_fence_is_signaled(fence))
    147  1.20  riastrad 		return false;
    148  1.20  riastrad 	dma_fence_put(fence);
    149  1.20  riastrad 	return true;
    150  1.20  riastrad }
    151   1.9  riastrad 
    152  1.20  riastrad static void
    153  1.20  riastrad nouveau_cli_work(struct work_struct *w)
    154  1.20  riastrad {
    155  1.20  riastrad 	struct nouveau_cli *cli = container_of(w, typeof(*cli), work);
    156  1.20  riastrad 	struct nouveau_cli_work *work, *wtmp;
    157  1.20  riastrad 	mutex_lock(&cli->lock);
    158  1.20  riastrad 	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
    159  1.20  riastrad 		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
    160  1.20  riastrad 			list_del(&work->head);
    161  1.20  riastrad 			work->func(work);
    162   1.9  riastrad 		}
    163   1.9  riastrad 	}
    164  1.20  riastrad 	mutex_unlock(&cli->lock);
    165   1.1  riastrad }
    166   1.1  riastrad 
    167   1.1  riastrad static void
    168  1.20  riastrad nouveau_cli_work_fence(struct dma_fence *fence, struct dma_fence_cb *cb)
    169  1.20  riastrad {
    170  1.20  riastrad 	struct nouveau_cli_work *work = container_of(cb, typeof(*work), cb);
    171  1.20  riastrad 	schedule_work(&work->cli->work);
    172  1.20  riastrad }
    173  1.20  riastrad 
    174  1.20  riastrad void
    175  1.20  riastrad nouveau_cli_work_queue(struct nouveau_cli *cli, struct dma_fence *fence,
    176  1.20  riastrad 		       struct nouveau_cli_work *work)
    177   1.1  riastrad {
    178  1.20  riastrad 	work->fence = dma_fence_get(fence);
    179  1.20  riastrad 	work->cli = cli;
    180  1.20  riastrad 	mutex_lock(&cli->lock);
    181  1.20  riastrad 	list_add_tail(&work->head, &cli->worker);
    182  1.20  riastrad 	if (dma_fence_add_callback(fence, &work->cb, nouveau_cli_work_fence))
    183  1.20  riastrad 		nouveau_cli_work_fence(fence, &work->cb);
    184  1.20  riastrad 	mutex_unlock(&cli->lock);
    185   1.1  riastrad }
    186   1.1  riastrad 
    187   1.1  riastrad static void
    188  1.20  riastrad nouveau_cli_fini(struct nouveau_cli *cli)
    189   1.1  riastrad {
    190  1.20  riastrad 	/* All our channels are dead now, which means all the fences they
    191  1.20  riastrad 	 * own are signalled, and all callback functions have been called.
    192  1.20  riastrad 	 *
    193  1.20  riastrad 	 * So, after flushing the workqueue, there should be nothing left.
    194  1.20  riastrad 	 */
    195  1.20  riastrad 	flush_work(&cli->work);
    196  1.20  riastrad 	WARN_ON(!list_empty(&cli->worker));
    197   1.9  riastrad 
    198  1.20  riastrad 	usif_client_fini(cli);
    199  1.20  riastrad 	nouveau_vmm_fini(&cli->svm);
    200  1.20  riastrad 	nouveau_vmm_fini(&cli->vmm);
    201  1.20  riastrad 	nvif_mmu_fini(&cli->mmu);
    202  1.20  riastrad 	nvif_device_fini(&cli->device);
    203  1.20  riastrad 	mutex_lock(&cli->drm->master.lock);
    204  1.20  riastrad 	nvif_client_fini(&cli->base);
    205  1.20  riastrad 	mutex_unlock(&cli->drm->master.lock);
    206  1.23  riastrad 	mutex_destroy(&cli->lock);
    207  1.23  riastrad 	mutex_destroy(&cli->mutex);
    208   1.1  riastrad }
    209   1.1  riastrad 
    210  1.20  riastrad static int
    211  1.20  riastrad nouveau_cli_init(struct nouveau_drm *drm, const char *sname,
    212  1.20  riastrad 		 struct nouveau_cli *cli)
    213   1.1  riastrad {
    214  1.20  riastrad 	static const struct nvif_mclass
    215  1.20  riastrad 	mems[] = {
    216  1.20  riastrad 		{ NVIF_CLASS_MEM_GF100, -1 },
    217  1.20  riastrad 		{ NVIF_CLASS_MEM_NV50 , -1 },
    218  1.20  riastrad 		{ NVIF_CLASS_MEM_NV04 , -1 },
    219  1.20  riastrad 		{}
    220  1.20  riastrad 	};
    221  1.20  riastrad 	static const struct nvif_mclass
    222  1.20  riastrad 	mmus[] = {
    223  1.20  riastrad 		{ NVIF_CLASS_MMU_GF100, -1 },
    224  1.20  riastrad 		{ NVIF_CLASS_MMU_NV50 , -1 },
    225  1.20  riastrad 		{ NVIF_CLASS_MMU_NV04 , -1 },
    226  1.20  riastrad 		{}
    227  1.20  riastrad 	};
    228  1.20  riastrad 	static const struct nvif_mclass
    229  1.20  riastrad 	vmms[] = {
    230  1.20  riastrad 		{ NVIF_CLASS_VMM_GP100, -1 },
    231  1.20  riastrad 		{ NVIF_CLASS_VMM_GM200, -1 },
    232  1.20  riastrad 		{ NVIF_CLASS_VMM_GF100, -1 },
    233  1.20  riastrad 		{ NVIF_CLASS_VMM_NV50 , -1 },
    234  1.20  riastrad 		{ NVIF_CLASS_VMM_NV04 , -1 },
    235  1.20  riastrad 		{}
    236  1.20  riastrad 	};
    237  1.20  riastrad 	u64 device = nouveau_name(drm->dev);
    238  1.20  riastrad 	int ret;
    239  1.20  riastrad 
    240  1.20  riastrad 	snprintf(cli->name, sizeof(cli->name), "%s", sname);
    241  1.20  riastrad 	cli->drm = drm;
    242  1.20  riastrad 	mutex_init(&cli->mutex);
    243  1.20  riastrad 	usif_client_init(cli);
    244  1.20  riastrad 
    245  1.20  riastrad 	INIT_WORK(&cli->work, nouveau_cli_work);
    246  1.20  riastrad 	INIT_LIST_HEAD(&cli->worker);
    247  1.20  riastrad 	mutex_init(&cli->lock);
    248  1.20  riastrad 
    249  1.20  riastrad 	if (cli == &drm->master) {
    250  1.20  riastrad 		ret = nvif_driver_init(NULL, nouveau_config, nouveau_debug,
    251  1.20  riastrad 				       cli->name, device, &cli->base);
    252  1.20  riastrad 	} else {
    253  1.20  riastrad 		mutex_lock(&drm->master.lock);
    254  1.20  riastrad 		ret = nvif_client_init(&drm->master.base, cli->name, device,
    255  1.20  riastrad 				       &cli->base);
    256  1.20  riastrad 		mutex_unlock(&drm->master.lock);
    257  1.20  riastrad 	}
    258  1.20  riastrad 	if (ret) {
    259  1.20  riastrad 		NV_PRINTK(err, cli, "Client allocation failed: %d\n", ret);
    260  1.20  riastrad 		goto done;
    261  1.20  riastrad 	}
    262  1.20  riastrad 
    263  1.20  riastrad 	ret = nvif_device_init(&cli->base.object, 0, NV_DEVICE,
    264  1.20  riastrad 			       &(struct nv_device_v0) {
    265  1.20  riastrad 					.device = ~0,
    266  1.20  riastrad 			       }, sizeof(struct nv_device_v0),
    267  1.20  riastrad 			       &cli->device);
    268  1.20  riastrad 	if (ret) {
    269  1.20  riastrad 		NV_PRINTK(err, cli, "Device allocation failed: %d\n", ret);
    270  1.20  riastrad 		goto done;
    271  1.20  riastrad 	}
    272   1.1  riastrad 
    273  1.20  riastrad 	ret = nvif_mclass(&cli->device.object, mmus);
    274  1.20  riastrad 	if (ret < 0) {
    275  1.20  riastrad 		NV_PRINTK(err, cli, "No supported MMU class\n");
    276  1.20  riastrad 		goto done;
    277  1.20  riastrad 	}
    278   1.1  riastrad 
    279  1.20  riastrad 	ret = nvif_mmu_init(&cli->device.object, mmus[ret].oclass, &cli->mmu);
    280  1.20  riastrad 	if (ret) {
    281  1.20  riastrad 		NV_PRINTK(err, cli, "MMU allocation failed: %d\n", ret);
    282  1.20  riastrad 		goto done;
    283  1.20  riastrad 	}
    284   1.9  riastrad 
    285  1.20  riastrad 	ret = nvif_mclass(&cli->mmu.object, vmms);
    286  1.20  riastrad 	if (ret < 0) {
    287  1.20  riastrad 		NV_PRINTK(err, cli, "No supported VMM class\n");
    288  1.20  riastrad 		goto done;
    289   1.9  riastrad 	}
    290   1.9  riastrad 
    291  1.20  riastrad 	ret = nouveau_vmm_init(cli, vmms[ret].oclass, &cli->vmm);
    292   1.1  riastrad 	if (ret) {
    293  1.20  riastrad 		NV_PRINTK(err, cli, "VMM allocation failed: %d\n", ret);
    294  1.20  riastrad 		goto done;
    295  1.20  riastrad 	}
    296  1.20  riastrad 
    297  1.20  riastrad 	ret = nvif_mclass(&cli->mmu.object, mems);
    298  1.20  riastrad 	if (ret < 0) {
    299  1.20  riastrad 		NV_PRINTK(err, cli, "No supported MEM class\n");
    300  1.20  riastrad 		goto done;
    301   1.1  riastrad 	}
    302   1.1  riastrad 
    303  1.20  riastrad 	cli->mem = &mems[ret];
    304  1.20  riastrad 	return 0;
    305  1.20  riastrad done:
    306  1.20  riastrad 	if (ret)
    307  1.20  riastrad 		nouveau_cli_fini(cli);
    308  1.20  riastrad 	return ret;
    309  1.20  riastrad }
    310  1.20  riastrad 
    311  1.20  riastrad static void
    312  1.20  riastrad nouveau_accel_ce_fini(struct nouveau_drm *drm)
    313  1.20  riastrad {
    314  1.20  riastrad 	nouveau_channel_idle(drm->cechan);
    315  1.20  riastrad 	nvif_object_fini(&drm->ttm.copy);
    316  1.20  riastrad 	nouveau_channel_del(&drm->cechan);
    317  1.20  riastrad }
    318  1.20  riastrad 
    319  1.20  riastrad static void
    320  1.20  riastrad nouveau_accel_ce_init(struct nouveau_drm *drm)
    321  1.20  riastrad {
    322  1.20  riastrad 	struct nvif_device *device = &drm->client.device;
    323  1.20  riastrad 	int ret = 0;
    324  1.20  riastrad 
    325  1.20  riastrad 	/* Allocate channel that has access to a (preferably async) copy
    326  1.20  riastrad 	 * engine, to use for TTM buffer moves.
    327  1.20  riastrad 	 */
    328   1.9  riastrad 	if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
    329  1.20  riastrad 		ret = nouveau_channel_new(drm, device,
    330  1.20  riastrad 					  nvif_fifo_runlist_ce(device), 0,
    331  1.20  riastrad 					  true, &drm->cechan);
    332   1.1  riastrad 	} else
    333   1.9  riastrad 	if (device->info.chipset >= 0xa3 &&
    334   1.9  riastrad 	    device->info.chipset != 0xaa &&
    335   1.9  riastrad 	    device->info.chipset != 0xac) {
    336  1.20  riastrad 		/* Prior to Kepler, there's only a single runlist, so all
    337  1.20  riastrad 		 * engines can be accessed from any channel.
    338  1.20  riastrad 		 *
    339  1.20  riastrad 		 * We still want to use a separate channel though.
    340  1.20  riastrad 		 */
    341  1.20  riastrad 		ret = nouveau_channel_new(drm, device, NvDmaFB, NvDmaTT, false,
    342  1.20  riastrad 					  &drm->cechan);
    343  1.20  riastrad 	}
    344  1.20  riastrad 
    345  1.20  riastrad 	if (ret)
    346  1.20  riastrad 		NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
    347  1.20  riastrad }
    348  1.20  riastrad 
    349  1.20  riastrad static void
    350  1.20  riastrad nouveau_accel_gr_fini(struct nouveau_drm *drm)
    351  1.20  riastrad {
    352  1.20  riastrad 	nouveau_channel_idle(drm->channel);
    353  1.20  riastrad 	nvif_object_fini(&drm->ntfy);
    354  1.20  riastrad 	nvkm_gpuobj_del(&drm->notify);
    355  1.20  riastrad 	nvif_object_fini(&drm->nvsw);
    356  1.20  riastrad 	nouveau_channel_del(&drm->channel);
    357  1.20  riastrad }
    358  1.20  riastrad 
    359  1.20  riastrad static void
    360  1.20  riastrad nouveau_accel_gr_init(struct nouveau_drm *drm)
    361  1.20  riastrad {
    362  1.20  riastrad 	struct nvif_device *device = &drm->client.device;
    363  1.20  riastrad 	u32 arg0, arg1;
    364  1.20  riastrad 	int ret;
    365   1.1  riastrad 
    366  1.20  riastrad 	/* Allocate channel that has access to the graphics engine. */
    367  1.20  riastrad 	if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
    368  1.20  riastrad 		arg0 = nvif_fifo_runlist(device, NV_DEVICE_INFO_ENGINE_GR);
    369  1.20  riastrad 		arg1 = 1;
    370   1.1  riastrad 	} else {
    371   1.1  riastrad 		arg0 = NvDmaFB;
    372   1.1  riastrad 		arg1 = NvDmaTT;
    373   1.1  riastrad 	}
    374   1.1  riastrad 
    375  1.20  riastrad 	ret = nouveau_channel_new(drm, device, arg0, arg1, false,
    376  1.20  riastrad 				  &drm->channel);
    377   1.1  riastrad 	if (ret) {
    378   1.1  riastrad 		NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
    379  1.20  riastrad 		nouveau_accel_gr_fini(drm);
    380   1.1  riastrad 		return;
    381   1.1  riastrad 	}
    382   1.1  riastrad 
    383  1.20  riastrad 	/* A SW class is used on pre-NV50 HW to assist with handling the
    384  1.20  riastrad 	 * synchronisation of page flips, as well as to implement fences
    385  1.20  riastrad 	 * on TNT/TNT2 HW that lacks any kind of support in host.
    386  1.20  riastrad 	 */
    387  1.20  riastrad 	if (device->info.family < NV_DEVICE_INFO_V0_TESLA) {
    388  1.20  riastrad 		ret = nvif_object_init(&drm->channel->user, NVDRM_NVSW,
    389  1.20  riastrad 				       nouveau_abi16_swclass(drm), NULL, 0,
    390  1.20  riastrad 				       &drm->nvsw);
    391   1.1  riastrad 		if (ret == 0) {
    392  1.20  riastrad 			ret = RING_SPACE(drm->channel, 2);
    393  1.20  riastrad 			if (ret == 0) {
    394   1.1  riastrad 				BEGIN_NV04(drm->channel, NvSubSw, 0, 1);
    395  1.20  riastrad 				OUT_RING  (drm->channel, drm->nvsw.handle);
    396   1.1  riastrad 			}
    397   1.1  riastrad 		}
    398   1.9  riastrad 
    399   1.9  riastrad 		if (ret) {
    400  1.20  riastrad 			NV_ERROR(drm, "failed to allocate sw class, %d\n", ret);
    401  1.20  riastrad 			nouveau_accel_gr_fini(drm);
    402   1.9  riastrad 			return;
    403   1.9  riastrad 		}
    404   1.1  riastrad 	}
    405   1.1  riastrad 
    406  1.20  riastrad 	/* NvMemoryToMemoryFormat requires a notifier ctxdma for some reason,
    407  1.20  riastrad 	 * even if notification is never requested, so, allocate a ctxdma on
    408  1.20  riastrad 	 * any GPU where it's possible we'll end up using M2MF for BO moves.
    409  1.20  riastrad 	 */
    410   1.9  riastrad 	if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
    411  1.20  riastrad 		ret = nvkm_gpuobj_new(nvxx_device(device), 32, 0, false, NULL,
    412  1.20  riastrad 				      &drm->notify);
    413   1.1  riastrad 		if (ret) {
    414   1.1  riastrad 			NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
    415  1.20  riastrad 			nouveau_accel_gr_fini(drm);
    416   1.1  riastrad 			return;
    417   1.1  riastrad 		}
    418   1.1  riastrad 
    419   1.9  riastrad 		ret = nvif_object_init(&drm->channel->user, NvNotify0,
    420   1.9  riastrad 				       NV_DMA_IN_MEMORY,
    421   1.9  riastrad 				       &(struct nv_dma_v0) {
    422   1.9  riastrad 						.target = NV_DMA_V0_TARGET_VRAM,
    423   1.9  riastrad 						.access = NV_DMA_V0_ACCESS_RDWR,
    424   1.1  riastrad 						.start = drm->notify->addr,
    425   1.1  riastrad 						.limit = drm->notify->addr + 31
    426   1.9  riastrad 				       }, sizeof(struct nv_dma_v0),
    427   1.9  riastrad 				       &drm->ntfy);
    428   1.1  riastrad 		if (ret) {
    429  1.20  riastrad 			nouveau_accel_gr_fini(drm);
    430   1.1  riastrad 			return;
    431   1.1  riastrad 		}
    432   1.1  riastrad 	}
    433  1.20  riastrad }
    434   1.1  riastrad 
    435  1.20  riastrad static void
    436  1.20  riastrad nouveau_accel_fini(struct nouveau_drm *drm)
    437  1.20  riastrad {
    438  1.20  riastrad 	nouveau_accel_ce_fini(drm);
    439  1.20  riastrad 	nouveau_accel_gr_fini(drm);
    440  1.20  riastrad 	if (drm->fence)
    441  1.20  riastrad 		nouveau_fence(drm)->dtor(drm);
    442   1.1  riastrad }
    443   1.1  riastrad 
    444  1.20  riastrad static void
    445  1.20  riastrad nouveau_accel_init(struct nouveau_drm *drm)
    446   1.1  riastrad {
    447  1.20  riastrad 	struct nvif_device *device = &drm->client.device;
    448  1.20  riastrad 	struct nvif_sclass *sclass;
    449  1.20  riastrad 	int ret, i, n;
    450  1.20  riastrad 
    451  1.20  riastrad 	if (nouveau_noaccel)
    452  1.20  riastrad 		return;
    453   1.1  riastrad 
    454  1.20  riastrad 	/* Initialise global support for channels, and synchronisation. */
    455  1.20  riastrad 	ret = nouveau_channels_init(drm);
    456   1.9  riastrad 	if (ret)
    457  1.20  riastrad 		return;
    458   1.9  riastrad 
    459  1.20  riastrad 	/*XXX: this is crap, but the fence/channel stuff is a little
    460  1.20  riastrad 	 *     backwards in some places.  this will be fixed.
    461  1.20  riastrad 	 */
    462  1.20  riastrad 	ret = n = nvif_object_sclass_get(&device->object, &sclass);
    463  1.20  riastrad 	if (ret < 0)
    464  1.20  riastrad 		return;
    465   1.9  riastrad 
    466  1.20  riastrad 	for (ret = -ENOSYS, i = 0; i < n; i++) {
    467  1.20  riastrad 		switch (sclass[i].oclass) {
    468  1.20  riastrad 		case NV03_CHANNEL_DMA:
    469  1.20  riastrad 			ret = nv04_fence_create(drm);
    470  1.20  riastrad 			break;
    471  1.20  riastrad 		case NV10_CHANNEL_DMA:
    472  1.20  riastrad 			ret = nv10_fence_create(drm);
    473  1.20  riastrad 			break;
    474  1.20  riastrad 		case NV17_CHANNEL_DMA:
    475  1.20  riastrad 		case NV40_CHANNEL_DMA:
    476  1.20  riastrad 			ret = nv17_fence_create(drm);
    477  1.20  riastrad 			break;
    478  1.20  riastrad 		case NV50_CHANNEL_GPFIFO:
    479  1.20  riastrad 			ret = nv50_fence_create(drm);
    480  1.20  riastrad 			break;
    481  1.20  riastrad 		case G82_CHANNEL_GPFIFO:
    482  1.20  riastrad 			ret = nv84_fence_create(drm);
    483  1.20  riastrad 			break;
    484  1.20  riastrad 		case FERMI_CHANNEL_GPFIFO:
    485  1.20  riastrad 		case KEPLER_CHANNEL_GPFIFO_A:
    486  1.20  riastrad 		case KEPLER_CHANNEL_GPFIFO_B:
    487  1.20  riastrad 		case MAXWELL_CHANNEL_GPFIFO_A:
    488  1.20  riastrad 		case PASCAL_CHANNEL_GPFIFO_A:
    489  1.20  riastrad 		case VOLTA_CHANNEL_GPFIFO_A:
    490  1.20  riastrad 		case TURING_CHANNEL_GPFIFO_A:
    491  1.20  riastrad 			ret = nvc0_fence_create(drm);
    492  1.20  riastrad 			break;
    493  1.20  riastrad 		default:
    494  1.20  riastrad 			break;
    495  1.20  riastrad 		}
    496   1.1  riastrad 	}
    497   1.1  riastrad 
    498  1.20  riastrad 	nvif_object_sclass_put(&sclass);
    499   1.1  riastrad 	if (ret) {
    500  1.20  riastrad 		NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
    501  1.20  riastrad 		nouveau_accel_fini(drm);
    502  1.20  riastrad 		return;
    503   1.1  riastrad 	}
    504   1.1  riastrad 
    505  1.20  riastrad 	/* Volta requires access to a doorbell register for kickoff. */
    506  1.20  riastrad 	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_VOLTA) {
    507  1.20  riastrad 		ret = nvif_user_init(device);
    508  1.20  riastrad 		if (ret)
    509  1.20  riastrad 			return;
    510   1.1  riastrad 	}
    511   1.1  riastrad 
    512  1.20  riastrad 	/* Allocate channels we need to support various functions. */
    513  1.20  riastrad 	nouveau_accel_gr_init(drm);
    514  1.20  riastrad 	nouveau_accel_ce_init(drm);
    515   1.1  riastrad 
    516  1.20  riastrad 	/* Initialise accelerated TTM buffer moves. */
    517  1.20  riastrad 	nouveau_bo_move_init(drm);
    518   1.1  riastrad }
    519   1.1  riastrad 
    520  1.21  riastrad int
    521  1.20  riastrad nouveau_drm_device_init(struct drm_device *dev)
    522   1.1  riastrad {
    523   1.1  riastrad 	struct nouveau_drm *drm;
    524   1.1  riastrad 	int ret;
    525   1.1  riastrad 
    526  1.20  riastrad 	if (!(drm = kzalloc(sizeof(*drm), GFP_KERNEL)))
    527  1.20  riastrad 		return -ENOMEM;
    528  1.20  riastrad 	dev->dev_private = drm;
    529  1.20  riastrad 	drm->dev = dev;
    530  1.20  riastrad 
    531  1.20  riastrad 	ret = nouveau_cli_init(drm, "DRM-master", &drm->master);
    532  1.20  riastrad 	if (ret)
    533  1.20  riastrad 		goto fail_alloc;
    534  1.20  riastrad 
    535  1.20  riastrad 	ret = nouveau_cli_init(drm, "DRM", &drm->client);
    536   1.1  riastrad 	if (ret)
    537  1.20  riastrad 		goto fail_master;
    538  1.20  riastrad 
    539  1.20  riastrad 	dev->irq_enabled = true;
    540   1.1  riastrad 
    541   1.9  riastrad 	nvxx_client(&drm->client.base)->debug =
    542   1.9  riastrad 		nvkm_dbgopt(nouveau_debug, "DRM");
    543   1.1  riastrad 
    544   1.1  riastrad 	INIT_LIST_HEAD(&drm->clients);
    545   1.1  riastrad 	spin_lock_init(&drm->tile.lock);
    546   1.1  riastrad 
    547   1.1  riastrad 	/* workaround an odd issue on nvc1 by disabling the device's
    548   1.1  riastrad 	 * nosnoop capability.  hopefully won't cause issues until a
    549   1.1  riastrad 	 * better fix is found - assuming there is one...
    550   1.1  riastrad 	 */
    551  1.20  riastrad 	if (drm->client.device.info.chipset == 0xc1)
    552  1.20  riastrad 		nvif_mask(&drm->client.device.object, 0x00088080, 0x00000800, 0x00000000);
    553   1.1  riastrad 
    554   1.1  riastrad 	nouveau_vga_init(drm);
    555   1.1  riastrad 
    556  1.14  riastrad #ifdef __NetBSD__
    557  1.14  riastrad     {
    558  1.14  riastrad 	/* XXX Kludge to make register subregion mapping work.  */
    559  1.14  riastrad 	struct nvkm_client *client = nvxx_client(&drm->client.base);
    560  1.21  riastrad 	struct nvkm_device *device = nvxx_device(&drm->client.device);
    561  1.14  riastrad 	client->mmiot = device->mmiot;
    562  1.14  riastrad 	client->mmioh = device->mmioh;
    563  1.14  riastrad 	client->mmioaddr = device->mmioaddr;
    564  1.14  riastrad 	client->mmiosz = device->mmiosz;
    565  1.14  riastrad     }
    566  1.14  riastrad #endif
    567  1.14  riastrad 
    568   1.1  riastrad 	ret = nouveau_ttm_init(drm);
    569   1.1  riastrad 	if (ret)
    570   1.1  riastrad 		goto fail_ttm;
    571   1.1  riastrad 
    572   1.1  riastrad 	ret = nouveau_bios_init(dev);
    573   1.1  riastrad 	if (ret)
    574   1.1  riastrad 		goto fail_bios;
    575   1.1  riastrad 
    576  1.20  riastrad 	nouveau_accel_init(drm);
    577  1.20  riastrad 
    578   1.1  riastrad 	ret = nouveau_display_create(dev);
    579   1.1  riastrad 	if (ret)
    580   1.1  riastrad 		goto fail_dispctor;
    581   1.1  riastrad 
    582   1.1  riastrad 	if (dev->mode_config.num_crtc) {
    583  1.20  riastrad 		ret = nouveau_display_init(dev, false, false);
    584   1.1  riastrad 		if (ret)
    585   1.1  riastrad 			goto fail_dispinit;
    586   1.1  riastrad 	}
    587   1.1  riastrad 
    588  1.20  riastrad 	nouveau_debugfs_init(drm);
    589   1.1  riastrad 	nouveau_hwmon_init(dev);
    590  1.20  riastrad 	nouveau_svm_init(drm);
    591  1.20  riastrad 	nouveau_dmem_init(drm);
    592   1.1  riastrad 	nouveau_fbcon_init(dev);
    593  1.20  riastrad 	nouveau_led_init(dev);
    594   1.1  riastrad 
    595  1.20  riastrad 	if (nouveau_pmops_runtime()) {
    596   1.1  riastrad 		pm_runtime_use_autosuspend(dev->dev);
    597   1.1  riastrad 		pm_runtime_set_autosuspend_delay(dev->dev, 5000);
    598   1.1  riastrad 		pm_runtime_set_active(dev->dev);
    599   1.1  riastrad 		pm_runtime_allow(dev->dev);
    600   1.1  riastrad 		pm_runtime_mark_last_busy(dev->dev);
    601   1.1  riastrad 		pm_runtime_put(dev->dev);
    602   1.1  riastrad 	}
    603  1.20  riastrad 
    604   1.1  riastrad 	return 0;
    605   1.1  riastrad 
    606   1.1  riastrad fail_dispinit:
    607   1.1  riastrad 	nouveau_display_destroy(dev);
    608   1.1  riastrad fail_dispctor:
    609  1.20  riastrad 	nouveau_accel_fini(drm);
    610   1.1  riastrad 	nouveau_bios_takedown(dev);
    611   1.1  riastrad fail_bios:
    612   1.1  riastrad 	nouveau_ttm_fini(drm);
    613   1.1  riastrad fail_ttm:
    614   1.1  riastrad 	nouveau_vga_fini(drm);
    615  1.23  riastrad 	spin_lock_destroy(&drm->tile.lock);
    616  1.20  riastrad 	nouveau_cli_fini(&drm->client);
    617  1.20  riastrad fail_master:
    618  1.20  riastrad 	nouveau_cli_fini(&drm->master);
    619  1.20  riastrad fail_alloc:
    620  1.20  riastrad 	kfree(drm);
    621   1.1  riastrad 	return ret;
    622   1.1  riastrad }
    623   1.1  riastrad 
    624  1.21  riastrad void
    625  1.20  riastrad nouveau_drm_device_fini(struct drm_device *dev)
    626   1.1  riastrad {
    627   1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    628   1.1  riastrad 
    629  1.20  riastrad 	if (nouveau_pmops_runtime()) {
    630  1.20  riastrad 		pm_runtime_get_sync(dev->dev);
    631  1.20  riastrad 		pm_runtime_forbid(dev->dev);
    632  1.20  riastrad 	}
    633  1.20  riastrad 
    634  1.20  riastrad 	nouveau_led_fini(dev);
    635   1.1  riastrad 	nouveau_fbcon_fini(dev);
    636  1.20  riastrad 	nouveau_dmem_fini(drm);
    637  1.20  riastrad 	nouveau_svm_fini(drm);
    638   1.1  riastrad 	nouveau_hwmon_fini(dev);
    639  1.20  riastrad 	nouveau_debugfs_fini(drm);
    640   1.1  riastrad 
    641   1.1  riastrad 	if (dev->mode_config.num_crtc)
    642  1.20  riastrad 		nouveau_display_fini(dev, false, false);
    643   1.1  riastrad 	nouveau_display_destroy(dev);
    644   1.1  riastrad 
    645  1.20  riastrad 	nouveau_accel_fini(drm);
    646   1.1  riastrad 	nouveau_bios_takedown(dev);
    647   1.1  riastrad 
    648   1.1  riastrad 	nouveau_ttm_fini(drm);
    649   1.1  riastrad 	nouveau_vga_fini(drm);
    650   1.1  riastrad 
    651  1.23  riastrad 	spin_lock_destroy(&drm->tile.lock);
    652  1.23  riastrad 
    653  1.20  riastrad 	nouveau_cli_fini(&drm->client);
    654  1.20  riastrad 	nouveau_cli_fini(&drm->master);
    655  1.20  riastrad 	kfree(drm);
    656  1.20  riastrad }
    657  1.20  riastrad 
    658  1.20  riastrad #ifndef __NetBSD__
    659  1.20  riastrad static int nouveau_drm_probe(struct pci_dev *pdev,
    660  1.20  riastrad 			     const struct pci_device_id *pent)
    661  1.20  riastrad {
    662  1.20  riastrad 	struct nvkm_device *device;
    663  1.20  riastrad 	struct drm_device *drm_dev;
    664  1.20  riastrad 	struct apertures_struct *aper;
    665  1.20  riastrad 	bool boot = false;
    666  1.20  riastrad 	int ret;
    667  1.20  riastrad 
    668  1.20  riastrad 	if (vga_switcheroo_client_probe_defer(pdev))
    669  1.20  riastrad 		return -EPROBE_DEFER;
    670  1.20  riastrad 
    671  1.20  riastrad 	/* We need to check that the chipset is supported before booting
    672  1.20  riastrad 	 * fbdev off the hardware, as there's no way to put it back.
    673  1.20  riastrad 	 */
    674  1.20  riastrad 	ret = nvkm_device_pci_new(pdev, nouveau_config, "error",
    675  1.20  riastrad 				  true, false, 0, &device);
    676  1.20  riastrad 	if (ret)
    677  1.20  riastrad 		return ret;
    678  1.20  riastrad 
    679  1.20  riastrad 	nvkm_device_del(&device);
    680  1.20  riastrad 
    681  1.20  riastrad 	/* Remove conflicting drivers (vesafb, efifb etc). */
    682  1.20  riastrad 	aper = alloc_apertures(3);
    683  1.20  riastrad 	if (!aper)
    684  1.20  riastrad 		return -ENOMEM;
    685  1.20  riastrad 
    686  1.20  riastrad 	aper->ranges[0].base = pci_resource_start(pdev, 1);
    687  1.20  riastrad 	aper->ranges[0].size = pci_resource_len(pdev, 1);
    688  1.20  riastrad 	aper->count = 1;
    689  1.20  riastrad 
    690  1.20  riastrad 	if (pci_resource_len(pdev, 2)) {
    691  1.20  riastrad 		aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
    692  1.20  riastrad 		aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
    693  1.20  riastrad 		aper->count++;
    694  1.20  riastrad 	}
    695  1.20  riastrad 
    696  1.20  riastrad 	if (pci_resource_len(pdev, 3)) {
    697  1.20  riastrad 		aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
    698  1.20  riastrad 		aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
    699  1.20  riastrad 		aper->count++;
    700  1.20  riastrad 	}
    701  1.20  riastrad 
    702  1.20  riastrad #ifdef CONFIG_X86
    703  1.20  riastrad 	boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
    704   1.3  riastrad #endif
    705  1.20  riastrad 	if (nouveau_modeset != 2)
    706  1.20  riastrad 		drm_fb_helper_remove_conflicting_framebuffers(aper, "nouveaufb", boot);
    707  1.20  riastrad 	kfree(aper);
    708  1.20  riastrad 
    709  1.20  riastrad 	ret = nvkm_device_pci_new(pdev, nouveau_config, nouveau_debug,
    710  1.20  riastrad 				  true, true, ~0ULL, &device);
    711  1.20  riastrad 	if (ret)
    712  1.20  riastrad 		return ret;
    713  1.20  riastrad 
    714  1.20  riastrad 	pci_set_master(pdev);
    715  1.20  riastrad 
    716  1.20  riastrad 	if (nouveau_atomic)
    717  1.20  riastrad 		driver_pci.driver_features |= DRIVER_ATOMIC;
    718  1.20  riastrad 
    719  1.20  riastrad 	drm_dev = drm_dev_alloc(&driver_pci, &pdev->dev);
    720  1.20  riastrad 	if (IS_ERR(drm_dev)) {
    721  1.20  riastrad 		ret = PTR_ERR(drm_dev);
    722  1.20  riastrad 		goto fail_nvkm;
    723  1.20  riastrad 	}
    724  1.20  riastrad 
    725  1.20  riastrad 	ret = pci_enable_device(pdev);
    726  1.20  riastrad 	if (ret)
    727  1.20  riastrad 		goto fail_drm;
    728  1.20  riastrad 
    729  1.20  riastrad 	drm_dev->pdev = pdev;
    730  1.20  riastrad 	pci_set_drvdata(pdev, drm_dev);
    731  1.20  riastrad 
    732  1.20  riastrad 	ret = nouveau_drm_device_init(drm_dev);
    733  1.20  riastrad 	if (ret)
    734  1.20  riastrad 		goto fail_pci;
    735  1.20  riastrad 
    736  1.20  riastrad 	ret = drm_dev_register(drm_dev, pent->driver_data);
    737  1.20  riastrad 	if (ret)
    738  1.20  riastrad 		goto fail_drm_dev_init;
    739  1.20  riastrad 
    740   1.1  riastrad 	return 0;
    741  1.20  riastrad 
    742  1.20  riastrad fail_drm_dev_init:
    743  1.20  riastrad 	nouveau_drm_device_fini(drm_dev);
    744  1.20  riastrad fail_pci:
    745  1.20  riastrad 	pci_disable_device(pdev);
    746  1.20  riastrad fail_drm:
    747  1.20  riastrad 	drm_dev_put(drm_dev);
    748  1.20  riastrad fail_nvkm:
    749  1.20  riastrad 	nvkm_device_del(&device);
    750  1.20  riastrad 	return ret;
    751   1.1  riastrad }
    752  1.20  riastrad #endif
    753   1.1  riastrad 
    754   1.3  riastrad #ifndef __NetBSD__		/* XXX nouveau detach */
    755   1.9  riastrad void
    756   1.9  riastrad nouveau_drm_device_remove(struct drm_device *dev)
    757   1.1  riastrad {
    758   1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    759   1.9  riastrad 	struct nvkm_client *client;
    760   1.9  riastrad 	struct nvkm_device *device;
    761   1.1  riastrad 
    762  1.20  riastrad 	drm_dev_unregister(dev);
    763  1.20  riastrad 
    764   1.1  riastrad 	dev->irq_enabled = false;
    765   1.9  riastrad 	client = nvxx_client(&drm->client.base);
    766   1.9  riastrad 	device = nvkm_device_find(client->device);
    767   1.1  riastrad 
    768  1.20  riastrad 	nouveau_drm_device_fini(dev);
    769  1.20  riastrad 	drm_dev_put(dev);
    770   1.9  riastrad 	nvkm_device_del(&device);
    771   1.9  riastrad }
    772   1.9  riastrad 
    773   1.9  riastrad static void
    774   1.9  riastrad nouveau_drm_remove(struct pci_dev *pdev)
    775   1.9  riastrad {
    776   1.9  riastrad 	struct drm_device *dev = pci_get_drvdata(pdev);
    777   1.9  riastrad 
    778   1.9  riastrad 	nouveau_drm_device_remove(dev);
    779  1.20  riastrad 	pci_disable_device(pdev);
    780   1.1  riastrad }
    781   1.3  riastrad #endif
    782   1.1  riastrad 
    783   1.1  riastrad static int
    784   1.1  riastrad nouveau_do_suspend(struct drm_device *dev, bool runtime)
    785   1.1  riastrad {
    786   1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    787   1.1  riastrad 	int ret;
    788   1.1  riastrad 
    789  1.20  riastrad 	nouveau_svm_suspend(drm);
    790  1.20  riastrad 	nouveau_dmem_suspend(drm);
    791  1.20  riastrad 	nouveau_led_suspend(dev);
    792  1.20  riastrad 
    793   1.9  riastrad 	if (dev->mode_config.num_crtc) {
    794  1.20  riastrad 		NV_DEBUG(drm, "suspending console...\n");
    795   1.9  riastrad 		nouveau_fbcon_set_suspend(dev, 1);
    796  1.20  riastrad 		NV_DEBUG(drm, "suspending display...\n");
    797   1.9  riastrad 		ret = nouveau_display_suspend(dev, runtime);
    798   1.1  riastrad 		if (ret)
    799   1.1  riastrad 			return ret;
    800   1.1  riastrad 	}
    801   1.1  riastrad 
    802  1.20  riastrad 	NV_DEBUG(drm, "evicting buffers...\n");
    803   1.1  riastrad 	ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
    804   1.1  riastrad 
    805  1.20  riastrad 	NV_DEBUG(drm, "waiting for kernel channels to go idle...\n");
    806   1.1  riastrad 	if (drm->cechan) {
    807   1.1  riastrad 		ret = nouveau_channel_idle(drm->cechan);
    808   1.1  riastrad 		if (ret)
    809   1.1  riastrad 			goto fail_display;
    810   1.1  riastrad 	}
    811   1.1  riastrad 
    812   1.1  riastrad 	if (drm->channel) {
    813   1.1  riastrad 		ret = nouveau_channel_idle(drm->channel);
    814   1.1  riastrad 		if (ret)
    815   1.1  riastrad 			goto fail_display;
    816   1.1  riastrad 	}
    817   1.1  riastrad 
    818  1.20  riastrad 	NV_DEBUG(drm, "suspending fence...\n");
    819   1.1  riastrad 	if (drm->fence && nouveau_fence(drm)->suspend) {
    820   1.1  riastrad 		if (!nouveau_fence(drm)->suspend(drm)) {
    821   1.1  riastrad 			ret = -ENOMEM;
    822   1.1  riastrad 			goto fail_display;
    823   1.1  riastrad 		}
    824   1.1  riastrad 	}
    825   1.1  riastrad 
    826  1.20  riastrad 	NV_DEBUG(drm, "suspending object tree...\n");
    827  1.20  riastrad 	ret = nvif_client_suspend(&drm->master.base);
    828   1.1  riastrad 	if (ret)
    829   1.1  riastrad 		goto fail_client;
    830   1.1  riastrad 
    831   1.1  riastrad 	return 0;
    832   1.1  riastrad 
    833   1.1  riastrad fail_client:
    834   1.1  riastrad 	if (drm->fence && nouveau_fence(drm)->resume)
    835   1.1  riastrad 		nouveau_fence(drm)->resume(drm);
    836   1.1  riastrad 
    837   1.1  riastrad fail_display:
    838   1.1  riastrad 	if (dev->mode_config.num_crtc) {
    839  1.20  riastrad 		NV_DEBUG(drm, "resuming display...\n");
    840   1.9  riastrad 		nouveau_display_resume(dev, runtime);
    841   1.1  riastrad 	}
    842   1.1  riastrad 	return ret;
    843   1.1  riastrad }
    844   1.1  riastrad 
    845   1.9  riastrad static int
    846   1.9  riastrad nouveau_do_resume(struct drm_device *dev, bool runtime)
    847   1.9  riastrad {
    848  1.20  riastrad 	int ret = 0;
    849   1.9  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    850   1.9  riastrad 
    851  1.20  riastrad 	NV_DEBUG(drm, "resuming object tree...\n");
    852  1.20  riastrad 	ret = nvif_client_resume(&drm->master.base);
    853  1.20  riastrad 	if (ret) {
    854  1.20  riastrad 		NV_ERROR(drm, "Client resume failed with error: %d\n", ret);
    855  1.20  riastrad 		return ret;
    856  1.20  riastrad 	}
    857   1.9  riastrad 
    858  1.20  riastrad 	NV_DEBUG(drm, "resuming fence...\n");
    859   1.9  riastrad 	if (drm->fence && nouveau_fence(drm)->resume)
    860   1.9  riastrad 		nouveau_fence(drm)->resume(drm);
    861   1.9  riastrad 
    862   1.9  riastrad 	nouveau_run_vbios_init(dev);
    863   1.9  riastrad 
    864   1.9  riastrad 	if (dev->mode_config.num_crtc) {
    865  1.20  riastrad 		NV_DEBUG(drm, "resuming display...\n");
    866   1.9  riastrad 		nouveau_display_resume(dev, runtime);
    867  1.20  riastrad 		NV_DEBUG(drm, "resuming console...\n");
    868   1.9  riastrad 		nouveau_fbcon_set_suspend(dev, 0);
    869   1.9  riastrad 	}
    870   1.9  riastrad 
    871  1.20  riastrad 	nouveau_led_resume(dev);
    872  1.20  riastrad 	nouveau_dmem_resume(drm);
    873  1.20  riastrad 	nouveau_svm_resume(drm);
    874   1.9  riastrad 	return 0;
    875   1.9  riastrad }
    876   1.9  riastrad 
    877   1.9  riastrad int
    878   1.8  riastrad #ifdef __NetBSD__
    879   1.9  riastrad nouveau_pmops_suspend(struct drm_device *drm_dev)
    880   1.8  riastrad #else
    881   1.9  riastrad nouveau_pmops_suspend(struct device *dev)
    882   1.8  riastrad #endif
    883   1.1  riastrad {
    884   1.8  riastrad #ifndef __NetBSD__
    885   1.1  riastrad 	struct pci_dev *pdev = to_pci_dev(dev);
    886   1.1  riastrad 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
    887   1.7  riastrad #endif
    888   1.1  riastrad 	int ret;
    889   1.1  riastrad 
    890   1.1  riastrad 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
    891   1.1  riastrad 	    drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
    892   1.1  riastrad 		return 0;
    893   1.1  riastrad 
    894   1.1  riastrad 	ret = nouveau_do_suspend(drm_dev, false);
    895   1.1  riastrad 	if (ret)
    896   1.1  riastrad 		return ret;
    897   1.1  riastrad 
    898   1.4  riastrad #ifndef __NetBSD__		/* pmf handles this for us.  */
    899   1.1  riastrad 	pci_save_state(pdev);
    900   1.1  riastrad 	pci_disable_device(pdev);
    901   1.1  riastrad 	pci_set_power_state(pdev, PCI_D3hot);
    902   1.4  riastrad #endif
    903   1.9  riastrad 	udelay(200);
    904   1.1  riastrad 	return 0;
    905   1.1  riastrad }
    906   1.1  riastrad 
    907   1.9  riastrad int
    908   1.8  riastrad #ifdef __NetBSD__
    909   1.9  riastrad nouveau_pmops_resume(struct drm_device *drm_dev)
    910   1.8  riastrad #else
    911   1.9  riastrad nouveau_pmops_resume(struct device *dev)
    912   1.8  riastrad #endif
    913   1.1  riastrad {
    914   1.8  riastrad #ifndef __NetBSD__
    915   1.1  riastrad 	struct pci_dev *pdev = to_pci_dev(dev);
    916   1.1  riastrad 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
    917  1.21  riastrad #endif
    918  1.11  riastrad 	int ret;
    919   1.1  riastrad 
    920   1.1  riastrad 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
    921   1.1  riastrad 	    drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
    922   1.1  riastrad 		return 0;
    923   1.1  riastrad 
    924   1.4  riastrad #ifndef __NetBSD__		/* pmf handles this for us */
    925   1.1  riastrad 	pci_set_power_state(pdev, PCI_D0);
    926   1.1  riastrad 	pci_restore_state(pdev);
    927   1.1  riastrad 	ret = pci_enable_device(pdev);
    928   1.1  riastrad 	if (ret)
    929   1.1  riastrad 		return ret;
    930   1.1  riastrad 	pci_set_master(pdev);
    931   1.4  riastrad #endif
    932   1.1  riastrad 
    933  1.20  riastrad 	ret = nouveau_do_resume(drm_dev, false);
    934  1.20  riastrad 
    935  1.20  riastrad 	/* Monitors may have been connected / disconnected during suspend */
    936  1.20  riastrad 	schedule_work(&nouveau_drm(drm_dev)->hpd_work);
    937  1.20  riastrad 
    938  1.20  riastrad 	return ret;
    939   1.9  riastrad }
    940   1.9  riastrad 
    941  1.22  riastrad #ifdef __NetBSD__		/* XXX nouveau pm */
    942  1.22  riastrad 
    943  1.22  riastrad bool
    944  1.22  riastrad nouveau_pmops_runtime(void)
    945  1.22  riastrad {
    946  1.22  riastrad 	return true;		/* XXX */
    947  1.22  riastrad }
    948  1.22  riastrad 
    949  1.22  riastrad #else
    950  1.22  riastrad 
    951   1.9  riastrad static int
    952   1.9  riastrad nouveau_pmops_freeze(struct device *dev)
    953   1.9  riastrad {
    954   1.9  riastrad 	struct pci_dev *pdev = to_pci_dev(dev);
    955   1.9  riastrad 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
    956   1.9  riastrad 	return nouveau_do_suspend(drm_dev, false);
    957   1.9  riastrad }
    958   1.1  riastrad 
    959   1.9  riastrad static int
    960   1.9  riastrad nouveau_pmops_thaw(struct device *dev)
    961   1.9  riastrad {
    962   1.9  riastrad 	struct pci_dev *pdev = to_pci_dev(dev);
    963   1.9  riastrad 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
    964   1.9  riastrad 	return nouveau_do_resume(drm_dev, false);
    965   1.1  riastrad }
    966   1.1  riastrad 
    967  1.20  riastrad bool
    968  1.20  riastrad nouveau_pmops_runtime(void)
    969  1.20  riastrad {
    970  1.20  riastrad 	if (nouveau_runtime_pm == -1)
    971  1.20  riastrad 		return nouveau_is_optimus() || nouveau_is_v1_dsm();
    972  1.20  riastrad 	return nouveau_runtime_pm == 1;
    973  1.20  riastrad }
    974  1.20  riastrad 
    975   1.9  riastrad static int
    976   1.9  riastrad nouveau_pmops_runtime_suspend(struct device *dev)
    977   1.1  riastrad {
    978   1.1  riastrad 	struct pci_dev *pdev = to_pci_dev(dev);
    979   1.1  riastrad 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
    980   1.1  riastrad 	int ret;
    981   1.1  riastrad 
    982  1.20  riastrad 	if (!nouveau_pmops_runtime()) {
    983   1.9  riastrad 		pm_runtime_forbid(dev);
    984   1.9  riastrad 		return -EBUSY;
    985   1.9  riastrad 	}
    986   1.1  riastrad 
    987   1.9  riastrad 	nouveau_switcheroo_optimus_dsm();
    988   1.9  riastrad 	ret = nouveau_do_suspend(drm_dev, true);
    989   1.9  riastrad 	pci_save_state(pdev);
    990   1.9  riastrad 	pci_disable_device(pdev);
    991   1.9  riastrad 	pci_ignore_hotplug(pdev);
    992   1.9  riastrad 	pci_set_power_state(pdev, PCI_D3cold);
    993   1.9  riastrad 	drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
    994   1.1  riastrad 	return ret;
    995   1.1  riastrad }
    996   1.1  riastrad 
    997   1.9  riastrad static int
    998   1.9  riastrad nouveau_pmops_runtime_resume(struct device *dev)
    999   1.1  riastrad {
   1000   1.1  riastrad 	struct pci_dev *pdev = to_pci_dev(dev);
   1001   1.1  riastrad 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
   1002  1.20  riastrad 	struct nouveau_drm *drm = nouveau_drm(drm_dev);
   1003  1.20  riastrad 	struct nvif_device *device = &nouveau_drm(drm_dev)->client.device;
   1004   1.1  riastrad 	int ret;
   1005   1.1  riastrad 
   1006  1.20  riastrad 	if (!nouveau_pmops_runtime()) {
   1007  1.20  riastrad 		pm_runtime_forbid(dev);
   1008  1.20  riastrad 		return -EBUSY;
   1009  1.20  riastrad 	}
   1010   1.9  riastrad 
   1011   1.9  riastrad 	pci_set_power_state(pdev, PCI_D0);
   1012   1.9  riastrad 	pci_restore_state(pdev);
   1013   1.9  riastrad 	ret = pci_enable_device(pdev);
   1014   1.1  riastrad 	if (ret)
   1015   1.1  riastrad 		return ret;
   1016   1.9  riastrad 	pci_set_master(pdev);
   1017   1.9  riastrad 
   1018   1.9  riastrad 	ret = nouveau_do_resume(drm_dev, true);
   1019  1.20  riastrad 	if (ret) {
   1020  1.20  riastrad 		NV_ERROR(drm, "resume failed with: %d\n", ret);
   1021  1.20  riastrad 		return ret;
   1022  1.20  riastrad 	}
   1023   1.9  riastrad 
   1024   1.9  riastrad 	/* do magic */
   1025   1.9  riastrad 	nvif_mask(&device->object, 0x088488, (1 << 25), (1 << 25));
   1026   1.9  riastrad 	drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
   1027  1.20  riastrad 
   1028  1.20  riastrad 	/* Monitors may have been connected / disconnected during suspend */
   1029  1.20  riastrad 	schedule_work(&nouveau_drm(drm_dev)->hpd_work);
   1030  1.20  riastrad 
   1031   1.9  riastrad 	return ret;
   1032   1.9  riastrad }
   1033   1.9  riastrad 
   1034   1.9  riastrad static int
   1035   1.9  riastrad nouveau_pmops_runtime_idle(struct device *dev)
   1036   1.9  riastrad {
   1037  1.20  riastrad 	if (!nouveau_pmops_runtime()) {
   1038   1.9  riastrad 		pm_runtime_forbid(dev);
   1039   1.9  riastrad 		return -EBUSY;
   1040   1.9  riastrad 	}
   1041   1.9  riastrad 
   1042   1.9  riastrad 	pm_runtime_mark_last_busy(dev);
   1043   1.9  riastrad 	pm_runtime_autosuspend(dev);
   1044   1.9  riastrad 	/* we don't want the main rpm_idle to call suspend - we want to autosuspend */
   1045   1.9  riastrad 	return 1;
   1046   1.1  riastrad }
   1047   1.3  riastrad #endif	/* XXX nouveau pm */
   1048   1.1  riastrad 
   1049   1.1  riastrad static int
   1050   1.1  riastrad nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
   1051   1.1  riastrad {
   1052   1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   1053   1.1  riastrad 	struct nouveau_cli *cli;
   1054   1.3  riastrad #ifdef __NetBSD__
   1055   1.3  riastrad 	const char name[] = "user";
   1056   1.3  riastrad #else
   1057   1.1  riastrad 	char name[32], tmpname[TASK_COMM_LEN];
   1058   1.3  riastrad #endif
   1059   1.1  riastrad 	int ret;
   1060   1.1  riastrad 
   1061   1.1  riastrad 	/* need to bring up power immediately if opening device */
   1062   1.1  riastrad 	ret = pm_runtime_get_sync(dev->dev);
   1063   1.1  riastrad 	if (ret < 0 && ret != -EACCES)
   1064   1.1  riastrad 		return ret;
   1065   1.1  riastrad 
   1066   1.3  riastrad #ifndef __NetBSD__
   1067   1.1  riastrad 	get_task_comm(tmpname, current);
   1068   1.1  riastrad 	snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
   1069   1.3  riastrad #endif
   1070   1.1  riastrad 
   1071  1.20  riastrad 	if (!(cli = kzalloc(sizeof(*cli), GFP_KERNEL))) {
   1072  1.20  riastrad 		ret = -ENOMEM;
   1073  1.20  riastrad 		goto done;
   1074  1.20  riastrad 	}
   1075   1.1  riastrad 
   1076  1.20  riastrad 	ret = nouveau_cli_init(drm, name, cli);
   1077   1.1  riastrad 	if (ret)
   1078  1.20  riastrad 		goto done;
   1079   1.1  riastrad 
   1080   1.9  riastrad 	cli->base.super = false;
   1081   1.9  riastrad 
   1082   1.1  riastrad 	fpriv->driver_priv = cli;
   1083   1.1  riastrad 
   1084   1.1  riastrad 	mutex_lock(&drm->client.mutex);
   1085   1.1  riastrad 	list_add(&cli->head, &drm->clients);
   1086   1.1  riastrad 	mutex_unlock(&drm->client.mutex);
   1087   1.1  riastrad 
   1088  1.20  riastrad done:
   1089  1.20  riastrad 	if (ret && cli) {
   1090  1.20  riastrad 		nouveau_cli_fini(cli);
   1091  1.20  riastrad 		kfree(cli);
   1092  1.20  riastrad 	}
   1093  1.20  riastrad 
   1094   1.1  riastrad 	pm_runtime_mark_last_busy(dev->dev);
   1095   1.1  riastrad 	pm_runtime_put_autosuspend(dev->dev);
   1096   1.1  riastrad 	return ret;
   1097   1.1  riastrad }
   1098   1.1  riastrad 
   1099   1.1  riastrad static void
   1100  1.20  riastrad nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
   1101   1.1  riastrad {
   1102   1.1  riastrad 	struct nouveau_cli *cli = nouveau_cli(fpriv);
   1103   1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   1104   1.1  riastrad 
   1105   1.1  riastrad 	pm_runtime_get_sync(dev->dev);
   1106   1.1  riastrad 
   1107   1.9  riastrad 	mutex_lock(&cli->mutex);
   1108   1.1  riastrad 	if (cli->abi16)
   1109   1.1  riastrad 		nouveau_abi16_fini(cli->abi16);
   1110   1.9  riastrad 	mutex_unlock(&cli->mutex);
   1111   1.1  riastrad 
   1112   1.1  riastrad 	mutex_lock(&drm->client.mutex);
   1113   1.1  riastrad 	list_del(&cli->head);
   1114   1.1  riastrad 	mutex_unlock(&drm->client.mutex);
   1115   1.1  riastrad 
   1116  1.20  riastrad 	nouveau_cli_fini(cli);
   1117  1.20  riastrad 	kfree(cli);
   1118   1.1  riastrad 	pm_runtime_mark_last_busy(dev->dev);
   1119   1.1  riastrad 	pm_runtime_put_autosuspend(dev->dev);
   1120   1.1  riastrad }
   1121   1.1  riastrad 
   1122   1.1  riastrad static const struct drm_ioctl_desc
   1123   1.1  riastrad nouveau_ioctls[] = {
   1124  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_RENDER_ALLOW),
   1125  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
   1126  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_RENDER_ALLOW),
   1127  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_RENDER_ALLOW),
   1128  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_RENDER_ALLOW),
   1129  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_RENDER_ALLOW),
   1130  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_RENDER_ALLOW),
   1131  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_SVM_INIT, nouveau_svmm_init, DRM_RENDER_ALLOW),
   1132  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_SVM_BIND, nouveau_svmm_bind, DRM_RENDER_ALLOW),
   1133  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_RENDER_ALLOW),
   1134  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_RENDER_ALLOW),
   1135  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_RENDER_ALLOW),
   1136  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_RENDER_ALLOW),
   1137  1.20  riastrad 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_RENDER_ALLOW),
   1138   1.1  riastrad };
   1139   1.1  riastrad 
   1140  1.16      maya #ifdef __NetBSD__
   1141  1.16      maya static int			/* XXX expose to ioc32 */
   1142  1.16      maya nouveau_ioctl_override(struct file *fp, unsigned long cmd, void *data)
   1143  1.16      maya {
   1144  1.16      maya 	struct drm_file *file = fp->f_data;
   1145  1.16      maya 	struct drm_device *dev = file->minor->dev;
   1146  1.16      maya 	int ret;
   1147  1.16      maya 
   1148  1.16      maya 	ret = pm_runtime_get_sync(dev->dev);
   1149  1.16      maya 	if (ret < 0 && ret != -EACCES)
   1150  1.17       mrg 		/* XXX errno Linux->NetBSD */
   1151  1.17       mrg 		return -ret;
   1152  1.16      maya 
   1153  1.16      maya 	switch (DRM_IOCTL_NR(cmd) - DRM_COMMAND_BASE) {
   1154  1.16      maya 	case DRM_NOUVEAU_NVIF:
   1155  1.17       mrg 		/* XXX errno Linux->NetBSD */
   1156  1.16      maya 		ret = -usif_ioctl(file, data, IOCPARM_LEN(cmd));
   1157  1.16      maya 		break;
   1158  1.16      maya 	default:
   1159  1.16      maya 		ret = drm_ioctl(fp, cmd, data);
   1160  1.16      maya 		break;
   1161  1.16      maya 	}
   1162  1.16      maya 
   1163  1.16      maya 	pm_runtime_mark_last_busy(dev->dev);
   1164  1.16      maya 	pm_runtime_put_autosuspend(dev->dev);
   1165  1.16      maya 	return ret;
   1166  1.16      maya }
   1167  1.16      maya #else
   1168   1.9  riastrad long
   1169   1.9  riastrad nouveau_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
   1170   1.1  riastrad {
   1171   1.9  riastrad 	struct drm_file *filp = file->private_data;
   1172   1.9  riastrad 	struct drm_device *dev = filp->minor->dev;
   1173   1.1  riastrad 	long ret;
   1174   1.1  riastrad 
   1175   1.1  riastrad 	ret = pm_runtime_get_sync(dev->dev);
   1176   1.1  riastrad 	if (ret < 0 && ret != -EACCES)
   1177   1.1  riastrad 		return ret;
   1178   1.1  riastrad 
   1179   1.9  riastrad 	switch (_IOC_NR(cmd) - DRM_COMMAND_BASE) {
   1180   1.9  riastrad 	case DRM_NOUVEAU_NVIF:
   1181   1.9  riastrad 		ret = usif_ioctl(filp, (void __user *)arg, _IOC_SIZE(cmd));
   1182   1.9  riastrad 		break;
   1183   1.9  riastrad 	default:
   1184   1.9  riastrad 		ret = drm_ioctl(file, cmd, arg);
   1185   1.9  riastrad 		break;
   1186   1.9  riastrad 	}
   1187   1.1  riastrad 
   1188   1.1  riastrad 	pm_runtime_mark_last_busy(dev->dev);
   1189   1.1  riastrad 	pm_runtime_put_autosuspend(dev->dev);
   1190   1.1  riastrad 	return ret;
   1191   1.1  riastrad }
   1192  1.16      maya #endif
   1193   1.9  riastrad 
   1194  1.16      maya #ifndef __NetBSD__
   1195   1.1  riastrad static const struct file_operations
   1196   1.1  riastrad nouveau_driver_fops = {
   1197   1.1  riastrad 	.owner = THIS_MODULE,
   1198   1.1  riastrad 	.open = drm_open,
   1199   1.1  riastrad 	.release = drm_release,
   1200   1.1  riastrad 	.unlocked_ioctl = nouveau_drm_ioctl,
   1201   1.1  riastrad 	.mmap = nouveau_ttm_mmap,
   1202   1.1  riastrad 	.poll = drm_poll,
   1203   1.1  riastrad 	.read = drm_read,
   1204   1.1  riastrad #if defined(CONFIG_COMPAT)
   1205   1.1  riastrad 	.compat_ioctl = nouveau_compat_ioctl,
   1206   1.1  riastrad #endif
   1207   1.1  riastrad 	.llseek = noop_llseek,
   1208   1.1  riastrad };
   1209   1.3  riastrad #endif
   1210   1.1  riastrad 
   1211   1.1  riastrad static struct drm_driver
   1212   1.9  riastrad driver_stub = {
   1213   1.1  riastrad 	.driver_features =
   1214  1.20  riastrad 		DRIVER_GEM | DRIVER_MODESET | DRIVER_RENDER
   1215  1.20  riastrad #if defined(CONFIG_NOUVEAU_LEGACY_CTX_SUPPORT)
   1216  1.20  riastrad 		| DRIVER_KMS_LEGACY_CONTEXT
   1217  1.20  riastrad #endif
   1218  1.20  riastrad 		,
   1219   1.1  riastrad 
   1220   1.1  riastrad 	.open = nouveau_drm_open,
   1221   1.1  riastrad 	.postclose = nouveau_drm_postclose,
   1222   1.1  riastrad 	.lastclose = nouveau_vga_lastclose,
   1223   1.1  riastrad 
   1224   1.1  riastrad #if defined(CONFIG_DEBUG_FS)
   1225  1.20  riastrad 	.debugfs_init = nouveau_drm_debugfs_init,
   1226   1.1  riastrad #endif
   1227   1.1  riastrad 
   1228   1.1  riastrad 	.enable_vblank = nouveau_display_vblank_enable,
   1229   1.1  riastrad 	.disable_vblank = nouveau_display_vblank_disable,
   1230   1.1  riastrad 	.get_scanout_position = nouveau_display_scanoutpos,
   1231  1.20  riastrad 	.get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos,
   1232   1.1  riastrad 
   1233   1.1  riastrad 	.ioctls = nouveau_ioctls,
   1234   1.1  riastrad 	.num_ioctls = ARRAY_SIZE(nouveau_ioctls),
   1235   1.6       mrg #ifdef __NetBSD__
   1236   1.6       mrg 	.fops = NULL,
   1237   1.6       mrg 	.mmap_object = &nouveau_ttm_mmap_object,
   1238   1.6       mrg 	.gem_uvm_ops = &nouveau_gem_uvm_ops,
   1239  1.16      maya 	.ioctl_override = nouveau_ioctl_override,
   1240   1.6       mrg #else
   1241   1.1  riastrad 	.fops = &nouveau_driver_fops,
   1242   1.3  riastrad #endif
   1243   1.1  riastrad 
   1244   1.1  riastrad 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
   1245   1.1  riastrad 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
   1246   1.1  riastrad 	.gem_prime_pin = nouveau_gem_prime_pin,
   1247   1.1  riastrad 	.gem_prime_unpin = nouveau_gem_prime_unpin,
   1248   1.1  riastrad 	.gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
   1249   1.1  riastrad 	.gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
   1250   1.1  riastrad 	.gem_prime_vmap = nouveau_gem_prime_vmap,
   1251   1.1  riastrad 	.gem_prime_vunmap = nouveau_gem_prime_vunmap,
   1252   1.1  riastrad 
   1253  1.20  riastrad 	.gem_free_object_unlocked = nouveau_gem_object_del,
   1254   1.1  riastrad 	.gem_open_object = nouveau_gem_object_open,
   1255   1.1  riastrad 	.gem_close_object = nouveau_gem_object_close,
   1256   1.1  riastrad 
   1257   1.1  riastrad 	.dumb_create = nouveau_display_dumb_create,
   1258   1.1  riastrad 	.dumb_map_offset = nouveau_display_dumb_map_offset,
   1259   1.1  riastrad 
   1260   1.1  riastrad 	.name = DRIVER_NAME,
   1261   1.1  riastrad 	.desc = DRIVER_DESC,
   1262   1.1  riastrad #ifdef GIT_REVISION
   1263   1.1  riastrad 	.date = GIT_REVISION,
   1264   1.1  riastrad #else
   1265   1.1  riastrad 	.date = DRIVER_DATE,
   1266   1.1  riastrad #endif
   1267   1.1  riastrad 	.major = DRIVER_MAJOR,
   1268   1.1  riastrad 	.minor = DRIVER_MINOR,
   1269   1.1  riastrad 	.patchlevel = DRIVER_PATCHLEVEL,
   1270   1.1  riastrad };
   1271   1.1  riastrad 
   1272   1.3  riastrad #ifndef __NetBSD__
   1273   1.1  riastrad static struct pci_device_id
   1274   1.1  riastrad nouveau_drm_pci_table[] = {
   1275   1.1  riastrad 	{
   1276   1.1  riastrad 		PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
   1277   1.1  riastrad 		.class = PCI_BASE_CLASS_DISPLAY << 16,
   1278   1.1  riastrad 		.class_mask  = 0xff << 16,
   1279   1.1  riastrad 	},
   1280   1.1  riastrad 	{
   1281   1.1  riastrad 		PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
   1282   1.1  riastrad 		.class = PCI_BASE_CLASS_DISPLAY << 16,
   1283   1.1  riastrad 		.class_mask  = 0xff << 16,
   1284   1.1  riastrad 	},
   1285   1.1  riastrad 	{}
   1286   1.1  riastrad };
   1287   1.3  riastrad #endif
   1288   1.1  riastrad 
   1289  1.11  riastrad #ifndef __NetBSD__
   1290   1.9  riastrad static void nouveau_display_options(void)
   1291   1.1  riastrad {
   1292   1.9  riastrad 	DRM_DEBUG_DRIVER("Loading Nouveau with parameters:\n");
   1293   1.1  riastrad 
   1294   1.9  riastrad 	DRM_DEBUG_DRIVER("... tv_disable   : %d\n", nouveau_tv_disable);
   1295   1.9  riastrad 	DRM_DEBUG_DRIVER("... ignorelid    : %d\n", nouveau_ignorelid);
   1296   1.9  riastrad 	DRM_DEBUG_DRIVER("... duallink     : %d\n", nouveau_duallink);
   1297   1.9  riastrad 	DRM_DEBUG_DRIVER("... nofbaccel    : %d\n", nouveau_nofbaccel);
   1298   1.9  riastrad 	DRM_DEBUG_DRIVER("... config       : %s\n", nouveau_config);
   1299   1.9  riastrad 	DRM_DEBUG_DRIVER("... debug        : %s\n", nouveau_debug);
   1300   1.9  riastrad 	DRM_DEBUG_DRIVER("... noaccel      : %d\n", nouveau_noaccel);
   1301   1.9  riastrad 	DRM_DEBUG_DRIVER("... modeset      : %d\n", nouveau_modeset);
   1302   1.9  riastrad 	DRM_DEBUG_DRIVER("... runpm        : %d\n", nouveau_runtime_pm);
   1303   1.9  riastrad 	DRM_DEBUG_DRIVER("... vram_pushbuf : %d\n", nouveau_vram_pushbuf);
   1304  1.20  riastrad 	DRM_DEBUG_DRIVER("... hdmimhz      : %d\n", nouveau_hdmimhz);
   1305   1.1  riastrad }
   1306  1.11  riastrad #endif
   1307   1.1  riastrad 
   1308   1.9  riastrad #ifndef __NetBSD__		/* XXX nouveau pm */
   1309   1.1  riastrad static const struct dev_pm_ops nouveau_pm_ops = {
   1310   1.1  riastrad 	.suspend = nouveau_pmops_suspend,
   1311   1.1  riastrad 	.resume = nouveau_pmops_resume,
   1312   1.1  riastrad 	.freeze = nouveau_pmops_freeze,
   1313   1.1  riastrad 	.thaw = nouveau_pmops_thaw,
   1314   1.1  riastrad 	.poweroff = nouveau_pmops_freeze,
   1315   1.1  riastrad 	.restore = nouveau_pmops_resume,
   1316   1.1  riastrad 	.runtime_suspend = nouveau_pmops_runtime_suspend,
   1317   1.1  riastrad 	.runtime_resume = nouveau_pmops_runtime_resume,
   1318   1.1  riastrad 	.runtime_idle = nouveau_pmops_runtime_idle,
   1319   1.1  riastrad };
   1320   1.3  riastrad #endif	/* XXX nouveau pm */
   1321   1.1  riastrad 
   1322   1.3  riastrad #ifndef __NetBSD__
   1323   1.1  riastrad static struct pci_driver
   1324   1.1  riastrad nouveau_drm_pci_driver = {
   1325   1.1  riastrad 	.name = "nouveau",
   1326   1.1  riastrad 	.id_table = nouveau_drm_pci_table,
   1327   1.1  riastrad 	.probe = nouveau_drm_probe,
   1328   1.1  riastrad 	.remove = nouveau_drm_remove,
   1329   1.1  riastrad 	.driver.pm = &nouveau_pm_ops,
   1330   1.1  riastrad };
   1331   1.1  riastrad 
   1332   1.9  riastrad struct drm_device *
   1333   1.9  riastrad nouveau_platform_device_create(const struct nvkm_device_tegra_func *func,
   1334   1.9  riastrad 			       struct platform_device *pdev,
   1335   1.9  riastrad 			       struct nvkm_device **pdevice)
   1336   1.1  riastrad {
   1337   1.9  riastrad 	struct drm_device *drm;
   1338   1.9  riastrad 	int err;
   1339   1.1  riastrad 
   1340   1.9  riastrad 	err = nvkm_device_tegra_new(func, pdev, nouveau_config, nouveau_debug,
   1341   1.9  riastrad 				    true, true, ~0ULL, pdevice);
   1342   1.9  riastrad 	if (err)
   1343   1.9  riastrad 		goto err_free;
   1344   1.1  riastrad 
   1345   1.9  riastrad 	drm = drm_dev_alloc(&driver_platform, &pdev->dev);
   1346  1.20  riastrad 	if (IS_ERR(drm)) {
   1347  1.20  riastrad 		err = PTR_ERR(drm);
   1348   1.9  riastrad 		goto err_free;
   1349   1.1  riastrad 	}
   1350   1.1  riastrad 
   1351  1.20  riastrad 	err = nouveau_drm_device_init(drm);
   1352  1.20  riastrad 	if (err)
   1353  1.20  riastrad 		goto err_put;
   1354   1.9  riastrad 
   1355   1.9  riastrad 	platform_set_drvdata(pdev, drm);
   1356   1.9  riastrad 
   1357   1.9  riastrad 	return drm;
   1358   1.9  riastrad 
   1359  1.20  riastrad err_put:
   1360  1.20  riastrad 	drm_dev_put(drm);
   1361   1.9  riastrad err_free:
   1362   1.9  riastrad 	nvkm_device_del(pdevice);
   1363   1.9  riastrad 
   1364   1.9  riastrad 	return ERR_PTR(err);
   1365   1.1  riastrad }
   1366   1.1  riastrad 
   1367   1.1  riastrad static int __init
   1368   1.1  riastrad nouveau_drm_init(void)
   1369   1.1  riastrad {
   1370   1.9  riastrad 	driver_pci = driver_stub;
   1371   1.9  riastrad 	driver_platform = driver_stub;
   1372   1.9  riastrad 
   1373   1.9  riastrad 	nouveau_display_options();
   1374   1.9  riastrad 
   1375   1.1  riastrad 	if (nouveau_modeset == -1) {
   1376   1.1  riastrad 		if (vgacon_text_force())
   1377   1.1  riastrad 			nouveau_modeset = 0;
   1378   1.1  riastrad 	}
   1379   1.1  riastrad 
   1380   1.1  riastrad 	if (!nouveau_modeset)
   1381   1.1  riastrad 		return 0;
   1382   1.1  riastrad 
   1383   1.9  riastrad #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
   1384   1.9  riastrad 	platform_driver_register(&nouveau_platform_driver);
   1385   1.9  riastrad #endif
   1386   1.9  riastrad 
   1387   1.1  riastrad 	nouveau_register_dsm_handler();
   1388  1.20  riastrad 	nouveau_backlight_ctor();
   1389  1.20  riastrad 
   1390  1.20  riastrad #ifdef CONFIG_PCI
   1391  1.20  riastrad 	return pci_register_driver(&nouveau_drm_pci_driver);
   1392  1.20  riastrad #else
   1393  1.20  riastrad 	return 0;
   1394  1.20  riastrad #endif
   1395   1.1  riastrad }
   1396   1.1  riastrad 
   1397   1.1  riastrad static void __exit
   1398   1.1  riastrad nouveau_drm_exit(void)
   1399   1.1  riastrad {
   1400   1.1  riastrad 	if (!nouveau_modeset)
   1401   1.1  riastrad 		return;
   1402   1.1  riastrad 
   1403  1.20  riastrad #ifdef CONFIG_PCI
   1404  1.20  riastrad 	pci_unregister_driver(&nouveau_drm_pci_driver);
   1405  1.20  riastrad #endif
   1406  1.20  riastrad 	nouveau_backlight_dtor();
   1407   1.1  riastrad 	nouveau_unregister_dsm_handler();
   1408   1.9  riastrad 
   1409   1.9  riastrad #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
   1410   1.9  riastrad 	platform_driver_unregister(&nouveau_platform_driver);
   1411   1.9  riastrad #endif
   1412  1.20  riastrad 	if (IS_ENABLED(CONFIG_DRM_NOUVEAU_SVM))
   1413  1.20  riastrad 		mmu_notifier_synchronize();
   1414   1.1  riastrad }
   1415   1.3  riastrad #endif
   1416   1.1  riastrad 
   1417   1.1  riastrad module_init(nouveau_drm_init);
   1418   1.1  riastrad module_exit(nouveau_drm_exit);
   1419   1.1  riastrad 
   1420   1.1  riastrad MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
   1421   1.1  riastrad MODULE_AUTHOR(DRIVER_AUTHOR);
   1422   1.1  riastrad MODULE_DESCRIPTION(DRIVER_DESC);
   1423   1.1  riastrad MODULE_LICENSE("GPL and additional rights");
   1424