drm_bufs.c 42 KB

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