drm_bufs.c 43 KB

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