drm_bufs.c 44 KB

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