drm_bufs.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. /**
  2. * \file drm_bufs.c
  3. * Generic buffer template
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. */
  8. /*
  9. * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
  10. *
  11. * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  12. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13. * All Rights Reserved.
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a
  16. * copy of this software and associated documentation files (the "Software"),
  17. * to deal in the Software without restriction, including without limitation
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. * and/or sell copies of the Software, and to permit persons to whom the
  20. * Software is furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice (including the next
  23. * paragraph) shall be included in all copies or substantial portions of the
  24. * Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  29. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  30. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  31. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32. * OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. #include <linux/vmalloc.h>
  35. #include <linux/slab.h>
  36. #include <linux/log2.h>
  37. #include <asm/shmparam.h>
  38. #include "drmP.h"
  39. resource_size_t drm_get_resource_start(struct drm_device *dev, unsigned int resource)
  40. {
  41. return pci_resource_start(dev->pdev, resource);
  42. }
  43. EXPORT_SYMBOL(drm_get_resource_start);
  44. resource_size_t drm_get_resource_len(struct drm_device *dev, unsigned int resource)
  45. {
  46. return pci_resource_len(dev->pdev, resource);
  47. }
  48. EXPORT_SYMBOL(drm_get_resource_len);
  49. static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
  50. struct drm_local_map *map)
  51. {
  52. struct drm_map_list *entry;
  53. list_for_each_entry(entry, &dev->maplist, head) {
  54. /*
  55. * Because the kernel-userspace ABI is fixed at a 32-bit offset
  56. * while PCI resources may live above that, we ignore the map
  57. * offset for maps of type _DRM_FRAMEBUFFER or _DRM_REGISTERS.
  58. * It is assumed that each driver will have only one resource of
  59. * each type.
  60. */
  61. if (!entry->map ||
  62. map->type != entry->map->type ||
  63. entry->master != dev->primary->master)
  64. continue;
  65. switch (map->type) {
  66. case _DRM_SHM:
  67. if (map->flags != _DRM_CONTAINS_LOCK)
  68. break;
  69. case _DRM_REGISTERS:
  70. case _DRM_FRAME_BUFFER:
  71. return entry;
  72. default: /* Make gcc happy */
  73. ;
  74. }
  75. if (entry->map->offset == map->offset)
  76. return entry;
  77. }
  78. return NULL;
  79. }
  80. static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
  81. unsigned long user_token, int hashed_handle, int shm)
  82. {
  83. int use_hashed_handle, shift;
  84. unsigned long add;
  85. #if (BITS_PER_LONG == 64)
  86. use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
  87. #elif (BITS_PER_LONG == 32)
  88. use_hashed_handle = hashed_handle;
  89. #else
  90. #error Unsupported long size. Neither 64 nor 32 bits.
  91. #endif
  92. if (!use_hashed_handle) {
  93. int ret;
  94. hash->key = user_token >> PAGE_SHIFT;
  95. ret = drm_ht_insert_item(&dev->map_hash, hash);
  96. if (ret != -EINVAL)
  97. return ret;
  98. }
  99. shift = 0;
  100. add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
  101. if (shm && (SHMLBA > PAGE_SIZE)) {
  102. int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
  103. /* For shared memory, we have to preserve the SHMLBA
  104. * bits of the eventual vma->vm_pgoff value during
  105. * mmap(). Otherwise we run into cache aliasing problems
  106. * on some platforms. On these platforms, the pgoff of
  107. * a mmap() request is used to pick a suitable virtual
  108. * address for the mmap() region such that it will not
  109. * cause cache aliasing problems.
  110. *
  111. * Therefore, make sure the SHMLBA relevant bits of the
  112. * hash value we use are equal to those in the original
  113. * kernel virtual address.
  114. */
  115. shift = bits;
  116. add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
  117. }
  118. return drm_ht_just_insert_please(&dev->map_hash, hash,
  119. user_token, 32 - PAGE_SHIFT - 3,
  120. shift, add);
  121. }
  122. /**
  123. * Core function to create a range of memory available for mapping by a
  124. * non-root process.
  125. *
  126. * Adjusts the memory offset to its absolute value according to the mapping
  127. * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
  128. * applicable and if supported by the kernel.
  129. */
  130. static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
  131. unsigned int size, enum drm_map_type type,
  132. enum drm_map_flags flags,
  133. struct drm_map_list ** maplist)
  134. {
  135. struct drm_local_map *map;
  136. struct drm_map_list *list;
  137. drm_dma_handle_t *dmah;
  138. unsigned long user_token;
  139. int ret;
  140. map = kmalloc(sizeof(*map), GFP_KERNEL);
  141. if (!map)
  142. return -ENOMEM;
  143. map->offset = offset;
  144. map->size = size;
  145. map->flags = flags;
  146. map->type = type;
  147. /* Only allow shared memory to be removable since we only keep enough
  148. * book keeping information about shared memory to allow for removal
  149. * when processes fork.
  150. */
  151. if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
  152. kfree(map);
  153. return -EINVAL;
  154. }
  155. DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
  156. (unsigned long long)map->offset, map->size, map->type);
  157. /* page-align _DRM_SHM maps. They are allocated here so there is no security
  158. * hole created by that and it works around various broken drivers that use
  159. * a non-aligned quantity to map the SAREA. --BenH
  160. */
  161. if (map->type == _DRM_SHM)
  162. map->size = PAGE_ALIGN(map->size);
  163. if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
  164. kfree(map);
  165. return -EINVAL;
  166. }
  167. map->mtrr = -1;
  168. map->handle = NULL;
  169. switch (map->type) {
  170. case _DRM_REGISTERS:
  171. case _DRM_FRAME_BUFFER:
  172. #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
  173. if (map->offset + (map->size-1) < map->offset ||
  174. map->offset < virt_to_phys(high_memory)) {
  175. kfree(map);
  176. return -EINVAL;
  177. }
  178. #endif
  179. #ifdef __alpha__
  180. map->offset += dev->hose->mem_space->start;
  181. #endif
  182. /* Some drivers preinitialize some maps, without the X Server
  183. * needing to be aware of it. Therefore, we just return success
  184. * when the server tries to create a duplicate map.
  185. */
  186. list = drm_find_matching_map(dev, map);
  187. if (list != NULL) {
  188. if (list->map->size != map->size) {
  189. DRM_DEBUG("Matching maps of type %d with "
  190. "mismatched sizes, (%ld vs %ld)\n",
  191. map->type, map->size,
  192. list->map->size);
  193. list->map->size = map->size;
  194. }
  195. kfree(map);
  196. *maplist = list;
  197. return 0;
  198. }
  199. if (drm_core_has_MTRR(dev)) {
  200. if (map->type == _DRM_FRAME_BUFFER ||
  201. (map->flags & _DRM_WRITE_COMBINING)) {
  202. map->mtrr = mtrr_add(map->offset, map->size,
  203. MTRR_TYPE_WRCOMB, 1);
  204. }
  205. }
  206. if (map->type == _DRM_REGISTERS) {
  207. map->handle = ioremap(map->offset, map->size);
  208. if (!map->handle) {
  209. kfree(map);
  210. return -ENOMEM;
  211. }
  212. }
  213. break;
  214. case _DRM_SHM:
  215. list = drm_find_matching_map(dev, map);
  216. if (list != NULL) {
  217. if(list->map->size != map->size) {
  218. DRM_DEBUG("Matching maps of type %d with "
  219. "mismatched sizes, (%ld vs %ld)\n",
  220. map->type, map->size, list->map->size);
  221. list->map->size = map->size;
  222. }
  223. kfree(map);
  224. *maplist = list;
  225. return 0;
  226. }
  227. map->handle = vmalloc_user(map->size);
  228. DRM_DEBUG("%lu %d %p\n",
  229. map->size, drm_order(map->size), map->handle);
  230. if (!map->handle) {
  231. kfree(map);
  232. return -ENOMEM;
  233. }
  234. map->offset = (unsigned long)map->handle;
  235. if (map->flags & _DRM_CONTAINS_LOCK) {
  236. /* Prevent a 2nd X Server from creating a 2nd lock */
  237. if (dev->primary->master->lock.hw_lock != NULL) {
  238. vfree(map->handle);
  239. kfree(map);
  240. return -EBUSY;
  241. }
  242. dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
  243. }
  244. break;
  245. case _DRM_AGP: {
  246. struct drm_agp_mem *entry;
  247. int valid = 0;
  248. if (!drm_core_has_AGP(dev)) {
  249. kfree(map);
  250. return -EINVAL;
  251. }
  252. #ifdef __alpha__
  253. map->offset += dev->hose->mem_space->start;
  254. #endif
  255. /* In some cases (i810 driver), user space may have already
  256. * added the AGP base itself, because dev->agp->base previously
  257. * only got set during AGP enable. So, only add the base
  258. * address if the map's offset isn't already within the
  259. * aperture.
  260. */
  261. if (map->offset < dev->agp->base ||
  262. map->offset > dev->agp->base +
  263. dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
  264. map->offset += dev->agp->base;
  265. }
  266. map->mtrr = dev->agp->agp_mtrr; /* for getmap */
  267. /* This assumes the DRM is in total control of AGP space.
  268. * It's not always the case as AGP can be in the control
  269. * of user space (i.e. i810 driver). So this loop will get
  270. * skipped and we double check that dev->agp->memory is
  271. * actually set as well as being invalid before EPERM'ing
  272. */
  273. list_for_each_entry(entry, &dev->agp->memory, head) {
  274. if ((map->offset >= entry->bound) &&
  275. (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
  276. valid = 1;
  277. break;
  278. }
  279. }
  280. if (!list_empty(&dev->agp->memory) && !valid) {
  281. kfree(map);
  282. return -EPERM;
  283. }
  284. DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
  285. (unsigned long long)map->offset, map->size);
  286. break;
  287. }
  288. case _DRM_GEM:
  289. DRM_ERROR("tried to addmap GEM object\n");
  290. break;
  291. case _DRM_SCATTER_GATHER:
  292. if (!dev->sg) {
  293. kfree(map);
  294. return -EINVAL;
  295. }
  296. map->offset += (unsigned long)dev->sg->virtual;
  297. break;
  298. case _DRM_CONSISTENT:
  299. /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
  300. * As we're limiting the address to 2^32-1 (or less),
  301. * casting it down to 32 bits is no problem, but we
  302. * need to point to a 64bit variable first. */
  303. dmah = drm_pci_alloc(dev, map->size, map->size);
  304. if (!dmah) {
  305. kfree(map);
  306. return -ENOMEM;
  307. }
  308. map->handle = dmah->vaddr;
  309. map->offset = (unsigned long)dmah->busaddr;
  310. kfree(dmah);
  311. break;
  312. default:
  313. kfree(map);
  314. return -EINVAL;
  315. }
  316. list = kmalloc(sizeof(*list), GFP_KERNEL);
  317. if (!list) {
  318. if (map->type == _DRM_REGISTERS)
  319. iounmap(map->handle);
  320. kfree(map);
  321. return -EINVAL;
  322. }
  323. memset(list, 0, sizeof(*list));
  324. list->map = map;
  325. mutex_lock(&dev->struct_mutex);
  326. list_add(&list->head, &dev->maplist);
  327. /* Assign a 32-bit handle */
  328. /* We do it here so that dev->struct_mutex protects the increment */
  329. user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
  330. map->offset;
  331. ret = drm_map_handle(dev, &list->hash, user_token, 0,
  332. (map->type == _DRM_SHM));
  333. if (ret) {
  334. if (map->type == _DRM_REGISTERS)
  335. iounmap(map->handle);
  336. kfree(map);
  337. kfree(list);
  338. mutex_unlock(&dev->struct_mutex);
  339. return ret;
  340. }
  341. list->user_token = list->hash.key << PAGE_SHIFT;
  342. mutex_unlock(&dev->struct_mutex);
  343. if (!(map->flags & _DRM_DRIVER))
  344. list->master = dev->primary->master;
  345. *maplist = list;
  346. return 0;
  347. }
  348. int drm_addmap(struct drm_device * dev, resource_size_t offset,
  349. unsigned int size, enum drm_map_type type,
  350. enum drm_map_flags flags, struct drm_local_map ** map_ptr)
  351. {
  352. struct drm_map_list *list;
  353. int rc;
  354. rc = drm_addmap_core(dev, offset, size, type, flags, &list);
  355. if (!rc)
  356. *map_ptr = list->map;
  357. return rc;
  358. }
  359. EXPORT_SYMBOL(drm_addmap);
  360. /**
  361. * Ioctl to specify a range of memory that is available for mapping by a
  362. * non-root process.
  363. *
  364. * \param inode device inode.
  365. * \param file_priv DRM file private.
  366. * \param cmd command.
  367. * \param arg pointer to a drm_map structure.
  368. * \return zero on success or a negative value on error.
  369. *
  370. */
  371. int drm_addmap_ioctl(struct drm_device *dev, void *data,
  372. struct drm_file *file_priv)
  373. {
  374. struct drm_map *map = data;
  375. struct drm_map_list *maplist;
  376. int err;
  377. if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
  378. return -EPERM;
  379. err = drm_addmap_core(dev, map->offset, map->size, map->type,
  380. map->flags, &maplist);
  381. if (err)
  382. return err;
  383. /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
  384. map->handle = (void *)(unsigned long)maplist->user_token;
  385. return 0;
  386. }
  387. /**
  388. * Remove a map private from list and deallocate resources if the mapping
  389. * isn't in use.
  390. *
  391. * Searches the map on drm_device::maplist, removes it from the list, see if
  392. * its being used, and free any associate resource (such as MTRR's) if it's not
  393. * being on use.
  394. *
  395. * \sa drm_addmap
  396. */
  397. int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
  398. {
  399. struct drm_map_list *r_list = NULL, *list_t;
  400. drm_dma_handle_t dmah;
  401. int found = 0;
  402. struct drm_master *master;
  403. /* Find the list entry for the map and remove it */
  404. list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
  405. if (r_list->map == map) {
  406. master = r_list->master;
  407. list_del(&r_list->head);
  408. drm_ht_remove_key(&dev->map_hash,
  409. r_list->user_token >> PAGE_SHIFT);
  410. kfree(r_list);
  411. found = 1;
  412. break;
  413. }
  414. }
  415. if (!found)
  416. return -EINVAL;
  417. switch (map->type) {
  418. case _DRM_REGISTERS:
  419. iounmap(map->handle);
  420. /* FALLTHROUGH */
  421. case _DRM_FRAME_BUFFER:
  422. if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
  423. int retcode;
  424. retcode = mtrr_del(map->mtrr, map->offset, map->size);
  425. DRM_DEBUG("mtrr_del=%d\n", retcode);
  426. }
  427. break;
  428. case _DRM_SHM:
  429. vfree(map->handle);
  430. if (master) {
  431. if (dev->sigdata.lock == master->lock.hw_lock)
  432. dev->sigdata.lock = NULL;
  433. master->lock.hw_lock = NULL; /* SHM removed */
  434. master->lock.file_priv = NULL;
  435. wake_up_interruptible_all(&master->lock.lock_queue);
  436. }
  437. break;
  438. case _DRM_AGP:
  439. case _DRM_SCATTER_GATHER:
  440. break;
  441. case _DRM_CONSISTENT:
  442. dmah.vaddr = map->handle;
  443. dmah.busaddr = map->offset;
  444. dmah.size = map->size;
  445. __drm_pci_free(dev, &dmah);
  446. break;
  447. case _DRM_GEM:
  448. DRM_ERROR("tried to rmmap GEM object\n");
  449. break;
  450. }
  451. kfree(map);
  452. return 0;
  453. }
  454. EXPORT_SYMBOL(drm_rmmap_locked);
  455. int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
  456. {
  457. int ret;
  458. mutex_lock(&dev->struct_mutex);
  459. ret = drm_rmmap_locked(dev, map);
  460. mutex_unlock(&dev->struct_mutex);
  461. return ret;
  462. }
  463. EXPORT_SYMBOL(drm_rmmap);
  464. /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
  465. * the last close of the device, and this is necessary for cleanup when things
  466. * exit uncleanly. Therefore, having userland manually remove mappings seems
  467. * like a pointless exercise since they're going away anyway.
  468. *
  469. * One use case might be after addmap is allowed for normal users for SHM and
  470. * gets used by drivers that the server doesn't need to care about. This seems
  471. * unlikely.
  472. *
  473. * \param inode device inode.
  474. * \param file_priv DRM file private.
  475. * \param cmd command.
  476. * \param arg pointer to a struct drm_map structure.
  477. * \return zero on success or a negative value on error.
  478. */
  479. int drm_rmmap_ioctl(struct drm_device *dev, void *data,
  480. struct drm_file *file_priv)
  481. {
  482. struct drm_map *request = data;
  483. struct drm_local_map *map = NULL;
  484. struct drm_map_list *r_list;
  485. int ret;
  486. mutex_lock(&dev->struct_mutex);
  487. list_for_each_entry(r_list, &dev->maplist, head) {
  488. if (r_list->map &&
  489. r_list->user_token == (unsigned long)request->handle &&
  490. r_list->map->flags & _DRM_REMOVABLE) {
  491. map = r_list->map;
  492. break;
  493. }
  494. }
  495. /* List has wrapped around to the head pointer, or its empty we didn't
  496. * find anything.
  497. */
  498. if (list_empty(&dev->maplist) || !map) {
  499. mutex_unlock(&dev->struct_mutex);
  500. return -EINVAL;
  501. }
  502. /* Register and framebuffer maps are permanent */
  503. if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
  504. mutex_unlock(&dev->struct_mutex);
  505. return 0;
  506. }
  507. ret = drm_rmmap_locked(dev, map);
  508. mutex_unlock(&dev->struct_mutex);
  509. return ret;
  510. }
  511. /**
  512. * Cleanup after an error on one of the addbufs() functions.
  513. *
  514. * \param dev DRM device.
  515. * \param entry buffer entry where the error occurred.
  516. *
  517. * Frees any pages and buffers associated with the given entry.
  518. */
  519. static void drm_cleanup_buf_error(struct drm_device * dev,
  520. struct drm_buf_entry * entry)
  521. {
  522. int i;
  523. if (entry->seg_count) {
  524. for (i = 0; i < entry->seg_count; i++) {
  525. if (entry->seglist[i]) {
  526. drm_pci_free(dev, entry->seglist[i]);
  527. }
  528. }
  529. kfree(entry->seglist);
  530. entry->seg_count = 0;
  531. }
  532. if (entry->buf_count) {
  533. for (i = 0; i < entry->buf_count; i++) {
  534. kfree(entry->buflist[i].dev_private);
  535. }
  536. kfree(entry->buflist);
  537. entry->buf_count = 0;
  538. }
  539. }
  540. #if __OS_HAS_AGP
  541. /**
  542. * Add AGP buffers for DMA transfers.
  543. *
  544. * \param dev struct drm_device to which the buffers are to be added.
  545. * \param request pointer to a struct drm_buf_desc describing the request.
  546. * \return zero on success or a negative number on failure.
  547. *
  548. * After some sanity checks creates a drm_buf structure for each buffer and
  549. * reallocates the buffer list of the same size order to accommodate the new
  550. * buffers.
  551. */
  552. int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
  553. {
  554. struct drm_device_dma *dma = dev->dma;
  555. struct drm_buf_entry *entry;
  556. struct drm_agp_mem *agp_entry;
  557. struct drm_buf *buf;
  558. unsigned long offset;
  559. unsigned long agp_offset;
  560. int count;
  561. int order;
  562. int size;
  563. int alignment;
  564. int page_order;
  565. int total;
  566. int byte_count;
  567. int i, valid;
  568. struct drm_buf **temp_buflist;
  569. if (!dma)
  570. return -EINVAL;
  571. count = request->count;
  572. order = drm_order(request->size);
  573. size = 1 << order;
  574. alignment = (request->flags & _DRM_PAGE_ALIGN)
  575. ? PAGE_ALIGN(size) : size;
  576. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  577. total = PAGE_SIZE << page_order;
  578. byte_count = 0;
  579. agp_offset = dev->agp->base + request->agp_start;
  580. DRM_DEBUG("count: %d\n", count);
  581. DRM_DEBUG("order: %d\n", order);
  582. DRM_DEBUG("size: %d\n", size);
  583. DRM_DEBUG("agp_offset: %lx\n", agp_offset);
  584. DRM_DEBUG("alignment: %d\n", alignment);
  585. DRM_DEBUG("page_order: %d\n", page_order);
  586. DRM_DEBUG("total: %d\n", total);
  587. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  588. return -EINVAL;
  589. if (dev->queue_count)
  590. return -EBUSY; /* Not while in use */
  591. /* Make sure buffers are located in AGP memory that we own */
  592. valid = 0;
  593. list_for_each_entry(agp_entry, &dev->agp->memory, head) {
  594. if ((agp_offset >= agp_entry->bound) &&
  595. (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
  596. valid = 1;
  597. break;
  598. }
  599. }
  600. if (!list_empty(&dev->agp->memory) && !valid) {
  601. DRM_DEBUG("zone invalid\n");
  602. return -EINVAL;
  603. }
  604. spin_lock(&dev->count_lock);
  605. if (dev->buf_use) {
  606. spin_unlock(&dev->count_lock);
  607. return -EBUSY;
  608. }
  609. atomic_inc(&dev->buf_alloc);
  610. spin_unlock(&dev->count_lock);
  611. mutex_lock(&dev->struct_mutex);
  612. entry = &dma->bufs[order];
  613. if (entry->buf_count) {
  614. mutex_unlock(&dev->struct_mutex);
  615. atomic_dec(&dev->buf_alloc);
  616. return -ENOMEM; /* May only call once for each order */
  617. }
  618. if (count < 0 || count > 4096) {
  619. mutex_unlock(&dev->struct_mutex);
  620. atomic_dec(&dev->buf_alloc);
  621. return -EINVAL;
  622. }
  623. entry->buflist = kmalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
  624. if (!entry->buflist) {
  625. mutex_unlock(&dev->struct_mutex);
  626. atomic_dec(&dev->buf_alloc);
  627. return -ENOMEM;
  628. }
  629. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  630. entry->buf_size = size;
  631. entry->page_order = page_order;
  632. offset = 0;
  633. while (entry->buf_count < count) {
  634. buf = &entry->buflist[entry->buf_count];
  635. buf->idx = dma->buf_count + entry->buf_count;
  636. buf->total = alignment;
  637. buf->order = order;
  638. buf->used = 0;
  639. buf->offset = (dma->byte_count + offset);
  640. buf->bus_address = agp_offset + offset;
  641. buf->address = (void *)(agp_offset + offset);
  642. buf->next = NULL;
  643. buf->waiting = 0;
  644. buf->pending = 0;
  645. init_waitqueue_head(&buf->dma_wait);
  646. buf->file_priv = NULL;
  647. buf->dev_priv_size = dev->driver->dev_priv_size;
  648. buf->dev_private = kmalloc(buf->dev_priv_size, GFP_KERNEL);
  649. if (!buf->dev_private) {
  650. /* Set count correctly so we free the proper amount. */
  651. entry->buf_count = count;
  652. drm_cleanup_buf_error(dev, entry);
  653. mutex_unlock(&dev->struct_mutex);
  654. atomic_dec(&dev->buf_alloc);
  655. return -ENOMEM;
  656. }
  657. memset(buf->dev_private, 0, buf->dev_priv_size);
  658. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  659. offset += alignment;
  660. entry->buf_count++;
  661. byte_count += PAGE_SIZE << page_order;
  662. }
  663. DRM_DEBUG("byte_count: %d\n", byte_count);
  664. temp_buflist = krealloc(dma->buflist,
  665. (dma->buf_count + entry->buf_count) *
  666. sizeof(*dma->buflist), GFP_KERNEL);
  667. if (!temp_buflist) {
  668. /* Free the entry because it isn't valid */
  669. drm_cleanup_buf_error(dev, entry);
  670. mutex_unlock(&dev->struct_mutex);
  671. atomic_dec(&dev->buf_alloc);
  672. return -ENOMEM;
  673. }
  674. dma->buflist = temp_buflist;
  675. for (i = 0; i < entry->buf_count; i++) {
  676. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  677. }
  678. dma->buf_count += entry->buf_count;
  679. dma->seg_count += entry->seg_count;
  680. dma->page_count += byte_count >> PAGE_SHIFT;
  681. dma->byte_count += byte_count;
  682. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  683. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  684. mutex_unlock(&dev->struct_mutex);
  685. request->count = entry->buf_count;
  686. request->size = size;
  687. dma->flags = _DRM_DMA_USE_AGP;
  688. atomic_dec(&dev->buf_alloc);
  689. return 0;
  690. }
  691. EXPORT_SYMBOL(drm_addbufs_agp);
  692. #endif /* __OS_HAS_AGP */
  693. int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
  694. {
  695. struct drm_device_dma *dma = dev->dma;
  696. int count;
  697. int order;
  698. int size;
  699. int total;
  700. int page_order;
  701. struct drm_buf_entry *entry;
  702. drm_dma_handle_t *dmah;
  703. struct drm_buf *buf;
  704. int alignment;
  705. unsigned long offset;
  706. int i;
  707. int byte_count;
  708. int page_count;
  709. unsigned long *temp_pagelist;
  710. struct drm_buf **temp_buflist;
  711. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
  712. return -EINVAL;
  713. if (!dma)
  714. return -EINVAL;
  715. if (!capable(CAP_SYS_ADMIN))
  716. return -EPERM;
  717. count = request->count;
  718. order = drm_order(request->size);
  719. size = 1 << order;
  720. DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
  721. request->count, request->size, size, order, dev->queue_count);
  722. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  723. return -EINVAL;
  724. if (dev->queue_count)
  725. return -EBUSY; /* Not while in use */
  726. alignment = (request->flags & _DRM_PAGE_ALIGN)
  727. ? PAGE_ALIGN(size) : size;
  728. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  729. total = PAGE_SIZE << page_order;
  730. spin_lock(&dev->count_lock);
  731. if (dev->buf_use) {
  732. spin_unlock(&dev->count_lock);
  733. return -EBUSY;
  734. }
  735. atomic_inc(&dev->buf_alloc);
  736. spin_unlock(&dev->count_lock);
  737. mutex_lock(&dev->struct_mutex);
  738. entry = &dma->bufs[order];
  739. if (entry->buf_count) {
  740. mutex_unlock(&dev->struct_mutex);
  741. atomic_dec(&dev->buf_alloc);
  742. return -ENOMEM; /* May only call once for each order */
  743. }
  744. if (count < 0 || count > 4096) {
  745. mutex_unlock(&dev->struct_mutex);
  746. atomic_dec(&dev->buf_alloc);
  747. return -EINVAL;
  748. }
  749. entry->buflist = kmalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
  750. if (!entry->buflist) {
  751. mutex_unlock(&dev->struct_mutex);
  752. atomic_dec(&dev->buf_alloc);
  753. return -ENOMEM;
  754. }
  755. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  756. entry->seglist = kmalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
  757. if (!entry->seglist) {
  758. kfree(entry->buflist);
  759. mutex_unlock(&dev->struct_mutex);
  760. atomic_dec(&dev->buf_alloc);
  761. return -ENOMEM;
  762. }
  763. memset(entry->seglist, 0, count * sizeof(*entry->seglist));
  764. /* Keep the original pagelist until we know all the allocations
  765. * have succeeded
  766. */
  767. temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
  768. sizeof(*dma->pagelist), GFP_KERNEL);
  769. if (!temp_pagelist) {
  770. kfree(entry->buflist);
  771. kfree(entry->seglist);
  772. mutex_unlock(&dev->struct_mutex);
  773. atomic_dec(&dev->buf_alloc);
  774. return -ENOMEM;
  775. }
  776. memcpy(temp_pagelist,
  777. dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
  778. DRM_DEBUG("pagelist: %d entries\n",
  779. dma->page_count + (count << page_order));
  780. entry->buf_size = size;
  781. entry->page_order = page_order;
  782. byte_count = 0;
  783. page_count = 0;
  784. while (entry->buf_count < count) {
  785. dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
  786. if (!dmah) {
  787. /* Set count correctly so we free the proper amount. */
  788. entry->buf_count = count;
  789. entry->seg_count = count;
  790. drm_cleanup_buf_error(dev, entry);
  791. kfree(temp_pagelist);
  792. mutex_unlock(&dev->struct_mutex);
  793. atomic_dec(&dev->buf_alloc);
  794. return -ENOMEM;
  795. }
  796. entry->seglist[entry->seg_count++] = dmah;
  797. for (i = 0; i < (1 << page_order); i++) {
  798. DRM_DEBUG("page %d @ 0x%08lx\n",
  799. dma->page_count + page_count,
  800. (unsigned long)dmah->vaddr + PAGE_SIZE * i);
  801. temp_pagelist[dma->page_count + page_count++]
  802. = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
  803. }
  804. for (offset = 0;
  805. offset + size <= total && entry->buf_count < count;
  806. offset += alignment, ++entry->buf_count) {
  807. buf = &entry->buflist[entry->buf_count];
  808. buf->idx = dma->buf_count + entry->buf_count;
  809. buf->total = alignment;
  810. buf->order = order;
  811. buf->used = 0;
  812. buf->offset = (dma->byte_count + byte_count + offset);
  813. buf->address = (void *)(dmah->vaddr + offset);
  814. buf->bus_address = dmah->busaddr + offset;
  815. buf->next = NULL;
  816. buf->waiting = 0;
  817. buf->pending = 0;
  818. init_waitqueue_head(&buf->dma_wait);
  819. buf->file_priv = NULL;
  820. buf->dev_priv_size = dev->driver->dev_priv_size;
  821. buf->dev_private = kmalloc(buf->dev_priv_size,
  822. GFP_KERNEL);
  823. if (!buf->dev_private) {
  824. /* Set count correctly so we free the proper amount. */
  825. entry->buf_count = count;
  826. entry->seg_count = count;
  827. drm_cleanup_buf_error(dev, entry);
  828. kfree(temp_pagelist);
  829. mutex_unlock(&dev->struct_mutex);
  830. atomic_dec(&dev->buf_alloc);
  831. return -ENOMEM;
  832. }
  833. memset(buf->dev_private, 0, buf->dev_priv_size);
  834. DRM_DEBUG("buffer %d @ %p\n",
  835. entry->buf_count, buf->address);
  836. }
  837. byte_count += PAGE_SIZE << page_order;
  838. }
  839. temp_buflist = krealloc(dma->buflist,
  840. (dma->buf_count + entry->buf_count) *
  841. sizeof(*dma->buflist), GFP_KERNEL);
  842. if (!temp_buflist) {
  843. /* Free the entry because it isn't valid */
  844. drm_cleanup_buf_error(dev, entry);
  845. kfree(temp_pagelist);
  846. mutex_unlock(&dev->struct_mutex);
  847. atomic_dec(&dev->buf_alloc);
  848. return -ENOMEM;
  849. }
  850. dma->buflist = temp_buflist;
  851. for (i = 0; i < entry->buf_count; i++) {
  852. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  853. }
  854. /* No allocations failed, so now we can replace the original pagelist
  855. * with the new one.
  856. */
  857. if (dma->page_count) {
  858. kfree(dma->pagelist);
  859. }
  860. dma->pagelist = temp_pagelist;
  861. dma->buf_count += entry->buf_count;
  862. dma->seg_count += entry->seg_count;
  863. dma->page_count += entry->seg_count << page_order;
  864. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  865. mutex_unlock(&dev->struct_mutex);
  866. request->count = entry->buf_count;
  867. request->size = size;
  868. if (request->flags & _DRM_PCI_BUFFER_RO)
  869. dma->flags = _DRM_DMA_USE_PCI_RO;
  870. atomic_dec(&dev->buf_alloc);
  871. return 0;
  872. }
  873. EXPORT_SYMBOL(drm_addbufs_pci);
  874. static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
  875. {
  876. struct drm_device_dma *dma = dev->dma;
  877. struct drm_buf_entry *entry;
  878. struct drm_buf *buf;
  879. unsigned long offset;
  880. unsigned long agp_offset;
  881. int count;
  882. int order;
  883. int size;
  884. int alignment;
  885. int page_order;
  886. int total;
  887. int byte_count;
  888. int i;
  889. struct drm_buf **temp_buflist;
  890. if (!drm_core_check_feature(dev, DRIVER_SG))
  891. return -EINVAL;
  892. if (!dma)
  893. return -EINVAL;
  894. if (!capable(CAP_SYS_ADMIN))
  895. return -EPERM;
  896. count = request->count;
  897. order = drm_order(request->size);
  898. size = 1 << order;
  899. alignment = (request->flags & _DRM_PAGE_ALIGN)
  900. ? PAGE_ALIGN(size) : size;
  901. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  902. total = PAGE_SIZE << page_order;
  903. byte_count = 0;
  904. agp_offset = request->agp_start;
  905. DRM_DEBUG("count: %d\n", count);
  906. DRM_DEBUG("order: %d\n", order);
  907. DRM_DEBUG("size: %d\n", size);
  908. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  909. DRM_DEBUG("alignment: %d\n", alignment);
  910. DRM_DEBUG("page_order: %d\n", page_order);
  911. DRM_DEBUG("total: %d\n", total);
  912. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  913. return -EINVAL;
  914. if (dev->queue_count)
  915. return -EBUSY; /* Not while in use */
  916. spin_lock(&dev->count_lock);
  917. if (dev->buf_use) {
  918. spin_unlock(&dev->count_lock);
  919. return -EBUSY;
  920. }
  921. atomic_inc(&dev->buf_alloc);
  922. spin_unlock(&dev->count_lock);
  923. mutex_lock(&dev->struct_mutex);
  924. entry = &dma->bufs[order];
  925. if (entry->buf_count) {
  926. mutex_unlock(&dev->struct_mutex);
  927. atomic_dec(&dev->buf_alloc);
  928. return -ENOMEM; /* May only call once for each order */
  929. }
  930. if (count < 0 || count > 4096) {
  931. mutex_unlock(&dev->struct_mutex);
  932. atomic_dec(&dev->buf_alloc);
  933. return -EINVAL;
  934. }
  935. entry->buflist = kmalloc(count * sizeof(*entry->buflist),
  936. GFP_KERNEL);
  937. if (!entry->buflist) {
  938. mutex_unlock(&dev->struct_mutex);
  939. atomic_dec(&dev->buf_alloc);
  940. return -ENOMEM;
  941. }
  942. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  943. entry->buf_size = size;
  944. entry->page_order = page_order;
  945. offset = 0;
  946. while (entry->buf_count < count) {
  947. buf = &entry->buflist[entry->buf_count];
  948. buf->idx = dma->buf_count + entry->buf_count;
  949. buf->total = alignment;
  950. buf->order = order;
  951. buf->used = 0;
  952. buf->offset = (dma->byte_count + offset);
  953. buf->bus_address = agp_offset + offset;
  954. buf->address = (void *)(agp_offset + offset
  955. + (unsigned long)dev->sg->virtual);
  956. buf->next = NULL;
  957. buf->waiting = 0;
  958. buf->pending = 0;
  959. init_waitqueue_head(&buf->dma_wait);
  960. buf->file_priv = NULL;
  961. buf->dev_priv_size = dev->driver->dev_priv_size;
  962. buf->dev_private = kmalloc(buf->dev_priv_size, GFP_KERNEL);
  963. if (!buf->dev_private) {
  964. /* Set count correctly so we free the proper amount. */
  965. entry->buf_count = count;
  966. drm_cleanup_buf_error(dev, entry);
  967. mutex_unlock(&dev->struct_mutex);
  968. atomic_dec(&dev->buf_alloc);
  969. return -ENOMEM;
  970. }
  971. memset(buf->dev_private, 0, buf->dev_priv_size);
  972. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  973. offset += alignment;
  974. entry->buf_count++;
  975. byte_count += PAGE_SIZE << page_order;
  976. }
  977. DRM_DEBUG("byte_count: %d\n", byte_count);
  978. temp_buflist = krealloc(dma->buflist,
  979. (dma->buf_count + entry->buf_count) *
  980. sizeof(*dma->buflist), GFP_KERNEL);
  981. if (!temp_buflist) {
  982. /* Free the entry because it isn't valid */
  983. drm_cleanup_buf_error(dev, entry);
  984. mutex_unlock(&dev->struct_mutex);
  985. atomic_dec(&dev->buf_alloc);
  986. return -ENOMEM;
  987. }
  988. dma->buflist = temp_buflist;
  989. for (i = 0; i < entry->buf_count; i++) {
  990. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  991. }
  992. dma->buf_count += entry->buf_count;
  993. dma->seg_count += entry->seg_count;
  994. dma->page_count += byte_count >> PAGE_SHIFT;
  995. dma->byte_count += byte_count;
  996. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  997. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  998. mutex_unlock(&dev->struct_mutex);
  999. request->count = entry->buf_count;
  1000. request->size = size;
  1001. dma->flags = _DRM_DMA_USE_SG;
  1002. atomic_dec(&dev->buf_alloc);
  1003. return 0;
  1004. }
  1005. static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
  1006. {
  1007. struct drm_device_dma *dma = dev->dma;
  1008. struct drm_buf_entry *entry;
  1009. struct drm_buf *buf;
  1010. unsigned long offset;
  1011. unsigned long agp_offset;
  1012. int count;
  1013. int order;
  1014. int size;
  1015. int alignment;
  1016. int page_order;
  1017. int total;
  1018. int byte_count;
  1019. int i;
  1020. struct drm_buf **temp_buflist;
  1021. if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
  1022. return -EINVAL;
  1023. if (!dma)
  1024. return -EINVAL;
  1025. if (!capable(CAP_SYS_ADMIN))
  1026. return -EPERM;
  1027. count = request->count;
  1028. order = drm_order(request->size);
  1029. size = 1 << order;
  1030. alignment = (request->flags & _DRM_PAGE_ALIGN)
  1031. ? PAGE_ALIGN(size) : size;
  1032. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  1033. total = PAGE_SIZE << page_order;
  1034. byte_count = 0;
  1035. agp_offset = request->agp_start;
  1036. DRM_DEBUG("count: %d\n", count);
  1037. DRM_DEBUG("order: %d\n", order);
  1038. DRM_DEBUG("size: %d\n", size);
  1039. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  1040. DRM_DEBUG("alignment: %d\n", alignment);
  1041. DRM_DEBUG("page_order: %d\n", page_order);
  1042. DRM_DEBUG("total: %d\n", total);
  1043. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  1044. return -EINVAL;
  1045. if (dev->queue_count)
  1046. return -EBUSY; /* Not while in use */
  1047. spin_lock(&dev->count_lock);
  1048. if (dev->buf_use) {
  1049. spin_unlock(&dev->count_lock);
  1050. return -EBUSY;
  1051. }
  1052. atomic_inc(&dev->buf_alloc);
  1053. spin_unlock(&dev->count_lock);
  1054. mutex_lock(&dev->struct_mutex);
  1055. entry = &dma->bufs[order];
  1056. if (entry->buf_count) {
  1057. mutex_unlock(&dev->struct_mutex);
  1058. atomic_dec(&dev->buf_alloc);
  1059. return -ENOMEM; /* May only call once for each order */
  1060. }
  1061. if (count < 0 || count > 4096) {
  1062. mutex_unlock(&dev->struct_mutex);
  1063. atomic_dec(&dev->buf_alloc);
  1064. return -EINVAL;
  1065. }
  1066. entry->buflist = kmalloc(count * sizeof(*entry->buflist),
  1067. GFP_KERNEL);
  1068. if (!entry->buflist) {
  1069. mutex_unlock(&dev->struct_mutex);
  1070. atomic_dec(&dev->buf_alloc);
  1071. return -ENOMEM;
  1072. }
  1073. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  1074. entry->buf_size = size;
  1075. entry->page_order = page_order;
  1076. offset = 0;
  1077. while (entry->buf_count < count) {
  1078. buf = &entry->buflist[entry->buf_count];
  1079. buf->idx = dma->buf_count + entry->buf_count;
  1080. buf->total = alignment;
  1081. buf->order = order;
  1082. buf->used = 0;
  1083. buf->offset = (dma->byte_count + offset);
  1084. buf->bus_address = agp_offset + offset;
  1085. buf->address = (void *)(agp_offset + offset);
  1086. buf->next = NULL;
  1087. buf->waiting = 0;
  1088. buf->pending = 0;
  1089. init_waitqueue_head(&buf->dma_wait);
  1090. buf->file_priv = NULL;
  1091. buf->dev_priv_size = dev->driver->dev_priv_size;
  1092. buf->dev_private = kmalloc(buf->dev_priv_size, GFP_KERNEL);
  1093. if (!buf->dev_private) {
  1094. /* Set count correctly so we free the proper amount. */
  1095. entry->buf_count = count;
  1096. drm_cleanup_buf_error(dev, entry);
  1097. mutex_unlock(&dev->struct_mutex);
  1098. atomic_dec(&dev->buf_alloc);
  1099. return -ENOMEM;
  1100. }
  1101. memset(buf->dev_private, 0, buf->dev_priv_size);
  1102. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  1103. offset += alignment;
  1104. entry->buf_count++;
  1105. byte_count += PAGE_SIZE << page_order;
  1106. }
  1107. DRM_DEBUG("byte_count: %d\n", byte_count);
  1108. temp_buflist = krealloc(dma->buflist,
  1109. (dma->buf_count + entry->buf_count) *
  1110. sizeof(*dma->buflist), GFP_KERNEL);
  1111. if (!temp_buflist) {
  1112. /* Free the entry because it isn't valid */
  1113. drm_cleanup_buf_error(dev, entry);
  1114. mutex_unlock(&dev->struct_mutex);
  1115. atomic_dec(&dev->buf_alloc);
  1116. return -ENOMEM;
  1117. }
  1118. dma->buflist = temp_buflist;
  1119. for (i = 0; i < entry->buf_count; i++) {
  1120. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  1121. }
  1122. dma->buf_count += entry->buf_count;
  1123. dma->seg_count += entry->seg_count;
  1124. dma->page_count += byte_count >> PAGE_SHIFT;
  1125. dma->byte_count += byte_count;
  1126. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  1127. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  1128. mutex_unlock(&dev->struct_mutex);
  1129. request->count = entry->buf_count;
  1130. request->size = size;
  1131. dma->flags = _DRM_DMA_USE_FB;
  1132. atomic_dec(&dev->buf_alloc);
  1133. return 0;
  1134. }
  1135. /**
  1136. * Add buffers for DMA transfers (ioctl).
  1137. *
  1138. * \param inode device inode.
  1139. * \param file_priv DRM file private.
  1140. * \param cmd command.
  1141. * \param arg pointer to a struct drm_buf_desc request.
  1142. * \return zero on success or a negative number on failure.
  1143. *
  1144. * According with the memory type specified in drm_buf_desc::flags and the
  1145. * build options, it dispatches the call either to addbufs_agp(),
  1146. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  1147. * PCI memory respectively.
  1148. */
  1149. int drm_addbufs(struct drm_device *dev, void *data,
  1150. struct drm_file *file_priv)
  1151. {
  1152. struct drm_buf_desc *request = data;
  1153. int ret;
  1154. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1155. return -EINVAL;
  1156. #if __OS_HAS_AGP
  1157. if (request->flags & _DRM_AGP_BUFFER)
  1158. ret = drm_addbufs_agp(dev, request);
  1159. else
  1160. #endif
  1161. if (request->flags & _DRM_SG_BUFFER)
  1162. ret = drm_addbufs_sg(dev, request);
  1163. else if (request->flags & _DRM_FB_BUFFER)
  1164. ret = drm_addbufs_fb(dev, request);
  1165. else
  1166. ret = drm_addbufs_pci(dev, request);
  1167. return ret;
  1168. }
  1169. /**
  1170. * Get information about the buffer mappings.
  1171. *
  1172. * This was originally mean for debugging purposes, or by a sophisticated
  1173. * client library to determine how best to use the available buffers (e.g.,
  1174. * large buffers can be used for image transfer).
  1175. *
  1176. * \param inode device inode.
  1177. * \param file_priv DRM file private.
  1178. * \param cmd command.
  1179. * \param arg pointer to a drm_buf_info structure.
  1180. * \return zero on success or a negative number on failure.
  1181. *
  1182. * Increments drm_device::buf_use while holding the drm_device::count_lock
  1183. * lock, preventing of allocating more buffers after this call. Information
  1184. * about each requested buffer is then copied into user space.
  1185. */
  1186. int drm_infobufs(struct drm_device *dev, void *data,
  1187. struct drm_file *file_priv)
  1188. {
  1189. struct drm_device_dma *dma = dev->dma;
  1190. struct drm_buf_info *request = data;
  1191. int i;
  1192. int count;
  1193. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1194. return -EINVAL;
  1195. if (!dma)
  1196. return -EINVAL;
  1197. spin_lock(&dev->count_lock);
  1198. if (atomic_read(&dev->buf_alloc)) {
  1199. spin_unlock(&dev->count_lock);
  1200. return -EBUSY;
  1201. }
  1202. ++dev->buf_use; /* Can't allocate more after this call */
  1203. spin_unlock(&dev->count_lock);
  1204. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1205. if (dma->bufs[i].buf_count)
  1206. ++count;
  1207. }
  1208. DRM_DEBUG("count = %d\n", count);
  1209. if (request->count >= count) {
  1210. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1211. if (dma->bufs[i].buf_count) {
  1212. struct drm_buf_desc __user *to =
  1213. &request->list[count];
  1214. struct drm_buf_entry *from = &dma->bufs[i];
  1215. struct drm_freelist *list = &dma->bufs[i].freelist;
  1216. if (copy_to_user(&to->count,
  1217. &from->buf_count,
  1218. sizeof(from->buf_count)) ||
  1219. copy_to_user(&to->size,
  1220. &from->buf_size,
  1221. sizeof(from->buf_size)) ||
  1222. copy_to_user(&to->low_mark,
  1223. &list->low_mark,
  1224. sizeof(list->low_mark)) ||
  1225. copy_to_user(&to->high_mark,
  1226. &list->high_mark,
  1227. sizeof(list->high_mark)))
  1228. return -EFAULT;
  1229. DRM_DEBUG("%d %d %d %d %d\n",
  1230. i,
  1231. dma->bufs[i].buf_count,
  1232. dma->bufs[i].buf_size,
  1233. dma->bufs[i].freelist.low_mark,
  1234. dma->bufs[i].freelist.high_mark);
  1235. ++count;
  1236. }
  1237. }
  1238. }
  1239. request->count = count;
  1240. return 0;
  1241. }
  1242. /**
  1243. * Specifies a low and high water mark for buffer allocation
  1244. *
  1245. * \param inode device inode.
  1246. * \param file_priv DRM file private.
  1247. * \param cmd command.
  1248. * \param arg a pointer to a drm_buf_desc structure.
  1249. * \return zero on success or a negative number on failure.
  1250. *
  1251. * Verifies that the size order is bounded between the admissible orders and
  1252. * updates the respective drm_device_dma::bufs entry low and high water mark.
  1253. *
  1254. * \note This ioctl is deprecated and mostly never used.
  1255. */
  1256. int drm_markbufs(struct drm_device *dev, void *data,
  1257. struct drm_file *file_priv)
  1258. {
  1259. struct drm_device_dma *dma = dev->dma;
  1260. struct drm_buf_desc *request = data;
  1261. int order;
  1262. struct drm_buf_entry *entry;
  1263. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1264. return -EINVAL;
  1265. if (!dma)
  1266. return -EINVAL;
  1267. DRM_DEBUG("%d, %d, %d\n",
  1268. request->size, request->low_mark, request->high_mark);
  1269. order = drm_order(request->size);
  1270. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  1271. return -EINVAL;
  1272. entry = &dma->bufs[order];
  1273. if (request->low_mark < 0 || request->low_mark > entry->buf_count)
  1274. return -EINVAL;
  1275. if (request->high_mark < 0 || request->high_mark > entry->buf_count)
  1276. return -EINVAL;
  1277. entry->freelist.low_mark = request->low_mark;
  1278. entry->freelist.high_mark = request->high_mark;
  1279. return 0;
  1280. }
  1281. /**
  1282. * Unreserve the buffers in list, previously reserved using drmDMA.
  1283. *
  1284. * \param inode device inode.
  1285. * \param file_priv DRM file private.
  1286. * \param cmd command.
  1287. * \param arg pointer to a drm_buf_free structure.
  1288. * \return zero on success or a negative number on failure.
  1289. *
  1290. * Calls free_buffer() for each used buffer.
  1291. * This function is primarily used for debugging.
  1292. */
  1293. int drm_freebufs(struct drm_device *dev, void *data,
  1294. struct drm_file *file_priv)
  1295. {
  1296. struct drm_device_dma *dma = dev->dma;
  1297. struct drm_buf_free *request = data;
  1298. int i;
  1299. int idx;
  1300. struct drm_buf *buf;
  1301. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1302. return -EINVAL;
  1303. if (!dma)
  1304. return -EINVAL;
  1305. DRM_DEBUG("%d\n", request->count);
  1306. for (i = 0; i < request->count; i++) {
  1307. if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
  1308. return -EFAULT;
  1309. if (idx < 0 || idx >= dma->buf_count) {
  1310. DRM_ERROR("Index %d (of %d max)\n",
  1311. idx, dma->buf_count - 1);
  1312. return -EINVAL;
  1313. }
  1314. buf = dma->buflist[idx];
  1315. if (buf->file_priv != file_priv) {
  1316. DRM_ERROR("Process %d freeing buffer not owned\n",
  1317. task_pid_nr(current));
  1318. return -EINVAL;
  1319. }
  1320. drm_free_buffer(dev, buf);
  1321. }
  1322. return 0;
  1323. }
  1324. /**
  1325. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1326. *
  1327. * \param inode device inode.
  1328. * \param file_priv DRM file private.
  1329. * \param cmd command.
  1330. * \param arg pointer to a drm_buf_map structure.
  1331. * \return zero on success or a negative number on failure.
  1332. *
  1333. * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
  1334. * about each buffer into user space. For PCI buffers, it calls do_mmap() with
  1335. * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
  1336. * drm_mmap_dma().
  1337. */
  1338. int drm_mapbufs(struct drm_device *dev, void *data,
  1339. struct drm_file *file_priv)
  1340. {
  1341. struct drm_device_dma *dma = dev->dma;
  1342. int retcode = 0;
  1343. const int zero = 0;
  1344. unsigned long virtual;
  1345. unsigned long address;
  1346. struct drm_buf_map *request = data;
  1347. int i;
  1348. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1349. return -EINVAL;
  1350. if (!dma)
  1351. return -EINVAL;
  1352. spin_lock(&dev->count_lock);
  1353. if (atomic_read(&dev->buf_alloc)) {
  1354. spin_unlock(&dev->count_lock);
  1355. return -EBUSY;
  1356. }
  1357. dev->buf_use++; /* Can't allocate more after this call */
  1358. spin_unlock(&dev->count_lock);
  1359. if (request->count >= dma->buf_count) {
  1360. if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
  1361. || (drm_core_check_feature(dev, DRIVER_SG)
  1362. && (dma->flags & _DRM_DMA_USE_SG))
  1363. || (drm_core_check_feature(dev, DRIVER_FB_DMA)
  1364. && (dma->flags & _DRM_DMA_USE_FB))) {
  1365. struct drm_local_map *map = dev->agp_buffer_map;
  1366. unsigned long token = dev->agp_buffer_token;
  1367. if (!map) {
  1368. retcode = -EINVAL;
  1369. goto done;
  1370. }
  1371. down_write(&current->mm->mmap_sem);
  1372. virtual = do_mmap(file_priv->filp, 0, map->size,
  1373. PROT_READ | PROT_WRITE,
  1374. MAP_SHARED,
  1375. token);
  1376. up_write(&current->mm->mmap_sem);
  1377. } else {
  1378. down_write(&current->mm->mmap_sem);
  1379. virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
  1380. PROT_READ | PROT_WRITE,
  1381. MAP_SHARED, 0);
  1382. up_write(&current->mm->mmap_sem);
  1383. }
  1384. if (virtual > -1024UL) {
  1385. /* Real error */
  1386. retcode = (signed long)virtual;
  1387. goto done;
  1388. }
  1389. request->virtual = (void __user *)virtual;
  1390. for (i = 0; i < dma->buf_count; i++) {
  1391. if (copy_to_user(&request->list[i].idx,
  1392. &dma->buflist[i]->idx,
  1393. sizeof(request->list[0].idx))) {
  1394. retcode = -EFAULT;
  1395. goto done;
  1396. }
  1397. if (copy_to_user(&request->list[i].total,
  1398. &dma->buflist[i]->total,
  1399. sizeof(request->list[0].total))) {
  1400. retcode = -EFAULT;
  1401. goto done;
  1402. }
  1403. if (copy_to_user(&request->list[i].used,
  1404. &zero, sizeof(zero))) {
  1405. retcode = -EFAULT;
  1406. goto done;
  1407. }
  1408. address = virtual + dma->buflist[i]->offset; /* *** */
  1409. if (copy_to_user(&request->list[i].address,
  1410. &address, sizeof(address))) {
  1411. retcode = -EFAULT;
  1412. goto done;
  1413. }
  1414. }
  1415. }
  1416. done:
  1417. request->count = dma->buf_count;
  1418. DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
  1419. return retcode;
  1420. }
  1421. /**
  1422. * Compute size order. Returns the exponent of the smaller power of two which
  1423. * is greater or equal to given number.
  1424. *
  1425. * \param size size.
  1426. * \return order.
  1427. *
  1428. * \todo Can be made faster.
  1429. */
  1430. int drm_order(unsigned long size)
  1431. {
  1432. int order;
  1433. unsigned long tmp;
  1434. for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
  1435. if (size & (size - 1))
  1436. ++order;
  1437. return order;
  1438. }
  1439. EXPORT_SYMBOL(drm_order);