1c0e09200SDave Airlie /* drm_pci.h -- PCI DMA memory management wrappers for DRM -*- linux-c -*- */ 2c0e09200SDave Airlie /** 3c0e09200SDave Airlie * \file drm_pci.c 4c0e09200SDave Airlie * \brief Functions and ioctls to manage PCI memory 5c0e09200SDave Airlie * 6c0e09200SDave Airlie * \warning These interfaces aren't stable yet. 7c0e09200SDave Airlie * 8c0e09200SDave Airlie * \todo Implement the remaining ioctl's for the PCI pools. 9c0e09200SDave Airlie * \todo The wrappers here are so thin that they would be better off inlined.. 10c0e09200SDave Airlie * 11c0e09200SDave Airlie * \author José Fonseca <jrfonseca@tungstengraphics.com> 12c0e09200SDave Airlie * \author Leif Delgass <ldelgass@retinalburn.net> 13c0e09200SDave Airlie */ 14c0e09200SDave Airlie 15c0e09200SDave Airlie /* 16c0e09200SDave Airlie * Copyright 2003 José Fonseca. 17c0e09200SDave Airlie * Copyright 2003 Leif Delgass. 18c0e09200SDave Airlie * All Rights Reserved. 19c0e09200SDave Airlie * 20c0e09200SDave Airlie * Permission is hereby granted, free of charge, to any person obtaining a 21c0e09200SDave Airlie * copy of this software and associated documentation files (the "Software"), 22c0e09200SDave Airlie * to deal in the Software without restriction, including without limitation 23c0e09200SDave Airlie * the rights to use, copy, modify, merge, publish, distribute, sublicense, 24c0e09200SDave Airlie * and/or sell copies of the Software, and to permit persons to whom the 25c0e09200SDave Airlie * Software is furnished to do so, subject to the following conditions: 26c0e09200SDave Airlie * 27c0e09200SDave Airlie * The above copyright notice and this permission notice (including the next 28c0e09200SDave Airlie * paragraph) shall be included in all copies or substantial portions of the 29c0e09200SDave Airlie * Software. 30c0e09200SDave Airlie * 31c0e09200SDave Airlie * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 32c0e09200SDave Airlie * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 33c0e09200SDave Airlie * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 34c0e09200SDave Airlie * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 35c0e09200SDave Airlie * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 36c0e09200SDave Airlie * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 37c0e09200SDave Airlie */ 38c0e09200SDave Airlie 39c0e09200SDave Airlie #include <linux/pci.h> 405a0e3ad6STejun Heo #include <linux/slab.h> 41c0e09200SDave Airlie #include <linux/dma-mapping.h> 42c0e09200SDave Airlie #include "drmP.h" 43c0e09200SDave Airlie 44c0e09200SDave Airlie /**********************************************************************/ 45c0e09200SDave Airlie /** \name PCI memory */ 46c0e09200SDave Airlie /*@{*/ 47c0e09200SDave Airlie 48c0e09200SDave Airlie /** 49c0e09200SDave Airlie * \brief Allocate a PCI consistent memory block, for DMA. 50c0e09200SDave Airlie */ 51e6be8d9dSZhenyu Wang drm_dma_handle_t *drm_pci_alloc(struct drm_device * dev, size_t size, size_t align) 52c0e09200SDave Airlie { 53c0e09200SDave Airlie drm_dma_handle_t *dmah; 54c0e09200SDave Airlie #if 1 55c0e09200SDave Airlie unsigned long addr; 56c0e09200SDave Airlie size_t sz; 57c0e09200SDave Airlie #endif 58c0e09200SDave Airlie 59c0e09200SDave Airlie /* pci_alloc_consistent only guarantees alignment to the smallest 60c0e09200SDave Airlie * PAGE_SIZE order which is greater than or equal to the requested size. 61c0e09200SDave Airlie * Return NULL here for now to make sure nobody tries for larger alignment 62c0e09200SDave Airlie */ 63c0e09200SDave Airlie if (align > size) 64c0e09200SDave Airlie return NULL; 65c0e09200SDave Airlie 66c0e09200SDave Airlie dmah = kmalloc(sizeof(drm_dma_handle_t), GFP_KERNEL); 67c0e09200SDave Airlie if (!dmah) 68c0e09200SDave Airlie return NULL; 69c0e09200SDave Airlie 70c0e09200SDave Airlie dmah->size = size; 71c0e09200SDave Airlie dmah->vaddr = dma_alloc_coherent(&dev->pdev->dev, size, &dmah->busaddr, GFP_KERNEL | __GFP_COMP); 72c0e09200SDave Airlie 73c0e09200SDave Airlie if (dmah->vaddr == NULL) { 74c0e09200SDave Airlie kfree(dmah); 75c0e09200SDave Airlie return NULL; 76c0e09200SDave Airlie } 77c0e09200SDave Airlie 78c0e09200SDave Airlie memset(dmah->vaddr, 0, size); 79c0e09200SDave Airlie 80c0e09200SDave Airlie /* XXX - Is virt_to_page() legal for consistent mem? */ 81c0e09200SDave Airlie /* Reserve */ 82c0e09200SDave Airlie for (addr = (unsigned long)dmah->vaddr, sz = size; 83c0e09200SDave Airlie sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) { 84c0e09200SDave Airlie SetPageReserved(virt_to_page(addr)); 85c0e09200SDave Airlie } 86c0e09200SDave Airlie 87c0e09200SDave Airlie return dmah; 88c0e09200SDave Airlie } 89c0e09200SDave Airlie 90c0e09200SDave Airlie EXPORT_SYMBOL(drm_pci_alloc); 91c0e09200SDave Airlie 92c0e09200SDave Airlie /** 93c0e09200SDave Airlie * \brief Free a PCI consistent memory block without freeing its descriptor. 94c0e09200SDave Airlie * 95c0e09200SDave Airlie * This function is for internal use in the Linux-specific DRM core code. 96c0e09200SDave Airlie */ 97c0e09200SDave Airlie void __drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah) 98c0e09200SDave Airlie { 99c0e09200SDave Airlie #if 1 100c0e09200SDave Airlie unsigned long addr; 101c0e09200SDave Airlie size_t sz; 102c0e09200SDave Airlie #endif 103c0e09200SDave Airlie 1049a298b2aSEric Anholt if (dmah->vaddr) { 105c0e09200SDave Airlie /* XXX - Is virt_to_page() legal for consistent mem? */ 106c0e09200SDave Airlie /* Unreserve */ 107c0e09200SDave Airlie for (addr = (unsigned long)dmah->vaddr, sz = dmah->size; 108c0e09200SDave Airlie sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) { 109c0e09200SDave Airlie ClearPageReserved(virt_to_page(addr)); 110c0e09200SDave Airlie } 111c0e09200SDave Airlie dma_free_coherent(&dev->pdev->dev, dmah->size, dmah->vaddr, 112c0e09200SDave Airlie dmah->busaddr); 113c0e09200SDave Airlie } 114c0e09200SDave Airlie } 115c0e09200SDave Airlie 116c0e09200SDave Airlie /** 117c0e09200SDave Airlie * \brief Free a PCI consistent memory block 118c0e09200SDave Airlie */ 119c0e09200SDave Airlie void drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah) 120c0e09200SDave Airlie { 121c0e09200SDave Airlie __drm_pci_free(dev, dmah); 122c0e09200SDave Airlie kfree(dmah); 123c0e09200SDave Airlie } 124c0e09200SDave Airlie 125c0e09200SDave Airlie EXPORT_SYMBOL(drm_pci_free); 126c0e09200SDave Airlie 127dcdb1674SJordan Crouse #ifdef CONFIG_PCI 128dcdb1674SJordan Crouse /** 129dcdb1674SJordan Crouse * Register. 130dcdb1674SJordan Crouse * 131dcdb1674SJordan Crouse * \param pdev - PCI device structure 132dcdb1674SJordan Crouse * \param ent entry from the PCI ID table with device type flags 133dcdb1674SJordan Crouse * \return zero on success or a negative number on failure. 134dcdb1674SJordan Crouse * 135dcdb1674SJordan Crouse * Attempt to gets inter module "drm" information. If we are first 136dcdb1674SJordan Crouse * then register the character device and inter module information. 137dcdb1674SJordan Crouse * Try and register, if we fail to register, backout previous work. 138dcdb1674SJordan Crouse */ 139dcdb1674SJordan Crouse int drm_get_pci_dev(struct pci_dev *pdev, const struct pci_device_id *ent, 140dcdb1674SJordan Crouse struct drm_driver *driver) 141dcdb1674SJordan Crouse { 142dcdb1674SJordan Crouse struct drm_device *dev; 143dcdb1674SJordan Crouse int ret; 144dcdb1674SJordan Crouse 145dcdb1674SJordan Crouse DRM_DEBUG("\n"); 146dcdb1674SJordan Crouse 147dcdb1674SJordan Crouse dev = kzalloc(sizeof(*dev), GFP_KERNEL); 148dcdb1674SJordan Crouse if (!dev) 149dcdb1674SJordan Crouse return -ENOMEM; 150dcdb1674SJordan Crouse 151dcdb1674SJordan Crouse ret = pci_enable_device(pdev); 152dcdb1674SJordan Crouse if (ret) 153dcdb1674SJordan Crouse goto err_g1; 154dcdb1674SJordan Crouse 155dcdb1674SJordan Crouse pci_set_master(pdev); 156dcdb1674SJordan Crouse 157dcdb1674SJordan Crouse dev->pdev = pdev; 158dcdb1674SJordan Crouse dev->dev = &pdev->dev; 159dcdb1674SJordan Crouse 160dcdb1674SJordan Crouse dev->pci_device = pdev->device; 161dcdb1674SJordan Crouse dev->pci_vendor = pdev->vendor; 162dcdb1674SJordan Crouse 163dcdb1674SJordan Crouse #ifdef __alpha__ 164dcdb1674SJordan Crouse dev->hose = pdev->sysdata; 165dcdb1674SJordan Crouse #endif 166dcdb1674SJordan Crouse 167b64c115eSDave Airlie mutex_lock(&drm_global_mutex); 168b64c115eSDave Airlie 169dcdb1674SJordan Crouse if ((ret = drm_fill_in_dev(dev, ent, driver))) { 170dcdb1674SJordan Crouse printk(KERN_ERR "DRM: Fill_in_dev failed.\n"); 171dcdb1674SJordan Crouse goto err_g2; 172dcdb1674SJordan Crouse } 173dcdb1674SJordan Crouse 174dcdb1674SJordan Crouse if (drm_core_check_feature(dev, DRIVER_MODESET)) { 175dcdb1674SJordan Crouse pci_set_drvdata(pdev, dev); 176dcdb1674SJordan Crouse ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL); 177dcdb1674SJordan Crouse if (ret) 178dcdb1674SJordan Crouse goto err_g2; 179dcdb1674SJordan Crouse } 180dcdb1674SJordan Crouse 181dcdb1674SJordan Crouse if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY))) 182dcdb1674SJordan Crouse goto err_g3; 183dcdb1674SJordan Crouse 184dcdb1674SJordan Crouse if (dev->driver->load) { 185dcdb1674SJordan Crouse ret = dev->driver->load(dev, ent->driver_data); 186dcdb1674SJordan Crouse if (ret) 187dcdb1674SJordan Crouse goto err_g4; 188dcdb1674SJordan Crouse } 189dcdb1674SJordan Crouse 190dcdb1674SJordan Crouse /* setup the grouping for the legacy output */ 191dcdb1674SJordan Crouse if (drm_core_check_feature(dev, DRIVER_MODESET)) { 192dcdb1674SJordan Crouse ret = drm_mode_group_init_legacy_group(dev, 193dcdb1674SJordan Crouse &dev->primary->mode_group); 194dcdb1674SJordan Crouse if (ret) 195dcdb1674SJordan Crouse goto err_g4; 196dcdb1674SJordan Crouse } 197dcdb1674SJordan Crouse 198dcdb1674SJordan Crouse list_add_tail(&dev->driver_item, &driver->device_list); 199dcdb1674SJordan Crouse 200dcdb1674SJordan Crouse DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n", 201dcdb1674SJordan Crouse driver->name, driver->major, driver->minor, driver->patchlevel, 202dcdb1674SJordan Crouse driver->date, pci_name(pdev), dev->primary->index); 203dcdb1674SJordan Crouse 204b64c115eSDave Airlie mutex_unlock(&drm_global_mutex); 205dcdb1674SJordan Crouse return 0; 206dcdb1674SJordan Crouse 207dcdb1674SJordan Crouse err_g4: 208dcdb1674SJordan Crouse drm_put_minor(&dev->primary); 209dcdb1674SJordan Crouse err_g3: 210dcdb1674SJordan Crouse if (drm_core_check_feature(dev, DRIVER_MODESET)) 211dcdb1674SJordan Crouse drm_put_minor(&dev->control); 212dcdb1674SJordan Crouse err_g2: 213dcdb1674SJordan Crouse pci_disable_device(pdev); 214dcdb1674SJordan Crouse err_g1: 215dcdb1674SJordan Crouse kfree(dev); 216b64c115eSDave Airlie mutex_unlock(&drm_global_mutex); 217dcdb1674SJordan Crouse return ret; 218dcdb1674SJordan Crouse } 219dcdb1674SJordan Crouse EXPORT_SYMBOL(drm_get_pci_dev); 220dcdb1674SJordan Crouse 221dcdb1674SJordan Crouse /** 222dcdb1674SJordan Crouse * PCI device initialization. Called via drm_init at module load time, 223dcdb1674SJordan Crouse * 224dcdb1674SJordan Crouse * \return zero on success or a negative number on failure. 225dcdb1674SJordan Crouse * 226dcdb1674SJordan Crouse * Initializes a drm_device structures,registering the 227dcdb1674SJordan Crouse * stubs and initializing the AGP device. 228dcdb1674SJordan Crouse * 229dcdb1674SJordan Crouse * Expands the \c DRIVER_PREINIT and \c DRIVER_POST_INIT macros before and 230dcdb1674SJordan Crouse * after the initialization for driver customization. 231dcdb1674SJordan Crouse */ 232dcdb1674SJordan Crouse int drm_pci_init(struct drm_driver *driver) 233dcdb1674SJordan Crouse { 234dcdb1674SJordan Crouse struct pci_dev *pdev = NULL; 235dcdb1674SJordan Crouse const struct pci_device_id *pid; 236dcdb1674SJordan Crouse int i; 237dcdb1674SJordan Crouse 238dcdb1674SJordan Crouse if (driver->driver_features & DRIVER_MODESET) 239dcdb1674SJordan Crouse return pci_register_driver(&driver->pci_driver); 240dcdb1674SJordan Crouse 241dcdb1674SJordan Crouse /* If not using KMS, fall back to stealth mode manual scanning. */ 242dcdb1674SJordan Crouse for (i = 0; driver->pci_driver.id_table[i].vendor != 0; i++) { 243dcdb1674SJordan Crouse pid = &driver->pci_driver.id_table[i]; 244dcdb1674SJordan Crouse 245dcdb1674SJordan Crouse /* Loop around setting up a DRM device for each PCI device 246dcdb1674SJordan Crouse * matching our ID and device class. If we had the internal 247dcdb1674SJordan Crouse * function that pci_get_subsys and pci_get_class used, we'd 248dcdb1674SJordan Crouse * be able to just pass pid in instead of doing a two-stage 249dcdb1674SJordan Crouse * thing. 250dcdb1674SJordan Crouse */ 251dcdb1674SJordan Crouse pdev = NULL; 252dcdb1674SJordan Crouse while ((pdev = 253dcdb1674SJordan Crouse pci_get_subsys(pid->vendor, pid->device, pid->subvendor, 254dcdb1674SJordan Crouse pid->subdevice, pdev)) != NULL) { 255dcdb1674SJordan Crouse if ((pdev->class & pid->class_mask) != pid->class) 256dcdb1674SJordan Crouse continue; 257dcdb1674SJordan Crouse 258dcdb1674SJordan Crouse /* stealth mode requires a manual probe */ 259dcdb1674SJordan Crouse pci_dev_get(pdev); 260dcdb1674SJordan Crouse drm_get_pci_dev(pdev, pid, driver); 261dcdb1674SJordan Crouse } 262dcdb1674SJordan Crouse } 263dcdb1674SJordan Crouse return 0; 264dcdb1674SJordan Crouse } 265dcdb1674SJordan Crouse 266dcdb1674SJordan Crouse #else 267dcdb1674SJordan Crouse 268dcdb1674SJordan Crouse int drm_pci_init(struct drm_driver *driver) 269dcdb1674SJordan Crouse { 270dcdb1674SJordan Crouse return -1; 271dcdb1674SJordan Crouse } 272dcdb1674SJordan Crouse 273dcdb1674SJordan Crouse #endif 274c0e09200SDave Airlie /*@}*/ 275