vmwgfx_cmdbuf.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /**************************************************************************
  2. *
  3. * Copyright © 2015 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_drv.h"
  28. #include "ttm/ttm_bo_api.h"
  29. /*
  30. * Size of inline command buffers. Try to make sure that a page size is a
  31. * multiple of the DMA pool allocation size.
  32. */
  33. #define VMW_CMDBUF_INLINE_ALIGN 64
  34. #define VMW_CMDBUF_INLINE_SIZE \
  35. (1024 - ALIGN(sizeof(SVGACBHeader), VMW_CMDBUF_INLINE_ALIGN))
  36. /**
  37. * struct vmw_cmdbuf_context - Command buffer context queues
  38. *
  39. * @submitted: List of command buffers that have been submitted to the
  40. * manager but not yet submitted to hardware.
  41. * @hw_submitted: List of command buffers submitted to hardware.
  42. * @preempted: List of preempted command buffers.
  43. * @num_hw_submitted: Number of buffers currently being processed by hardware
  44. */
  45. struct vmw_cmdbuf_context {
  46. struct list_head submitted;
  47. struct list_head hw_submitted;
  48. struct list_head preempted;
  49. unsigned num_hw_submitted;
  50. };
  51. /**
  52. * struct vmw_cmdbuf_man: - Command buffer manager
  53. *
  54. * @cur_mutex: Mutex protecting the command buffer used for incremental small
  55. * kernel command submissions, @cur.
  56. * @space_mutex: Mutex to protect against starvation when we allocate
  57. * main pool buffer space.
  58. * @work: A struct work_struct implementeing command buffer error handling.
  59. * Immutable.
  60. * @dev_priv: Pointer to the device private struct. Immutable.
  61. * @ctx: Array of command buffer context queues. The queues and the context
  62. * data is protected by @lock.
  63. * @error: List of command buffers that have caused device errors.
  64. * Protected by @lock.
  65. * @mm: Range manager for the command buffer space. Manager allocations and
  66. * frees are protected by @lock.
  67. * @cmd_space: Buffer object for the command buffer space, unless we were
  68. * able to make a contigous coherent DMA memory allocation, @handle. Immutable.
  69. * @map_obj: Mapping state for @cmd_space. Immutable.
  70. * @map: Pointer to command buffer space. May be a mapped buffer object or
  71. * a contigous coherent DMA memory allocation. Immutable.
  72. * @cur: Command buffer for small kernel command submissions. Protected by
  73. * the @cur_mutex.
  74. * @cur_pos: Space already used in @cur. Protected by @cur_mutex.
  75. * @default_size: Default size for the @cur command buffer. Immutable.
  76. * @max_hw_submitted: Max number of in-flight command buffers the device can
  77. * handle. Immutable.
  78. * @lock: Spinlock protecting command submission queues.
  79. * @header: Pool of DMA memory for device command buffer headers.
  80. * Internal protection.
  81. * @dheaders: Pool of DMA memory for device command buffer headers with trailing
  82. * space for inline data. Internal protection.
  83. * @tasklet: Tasklet struct for irq processing. Immutable.
  84. * @alloc_queue: Wait queue for processes waiting to allocate command buffer
  85. * space.
  86. * @idle_queue: Wait queue for processes waiting for command buffer idle.
  87. * @irq_on: Whether the process function has requested irq to be turned on.
  88. * Protected by @lock.
  89. * @using_mob: Whether the command buffer space is a MOB or a contigous DMA
  90. * allocation. Immutable.
  91. * @has_pool: Has a large pool of DMA memory which allows larger allocations.
  92. * Typically this is false only during bootstrap.
  93. * @handle: DMA address handle for the command buffer space if @using_mob is
  94. * false. Immutable.
  95. * @size: The size of the command buffer space. Immutable.
  96. */
  97. struct vmw_cmdbuf_man {
  98. struct mutex cur_mutex;
  99. struct mutex space_mutex;
  100. struct work_struct work;
  101. struct vmw_private *dev_priv;
  102. struct vmw_cmdbuf_context ctx[SVGA_CB_CONTEXT_MAX];
  103. struct list_head error;
  104. struct drm_mm mm;
  105. struct ttm_buffer_object *cmd_space;
  106. struct ttm_bo_kmap_obj map_obj;
  107. u8 *map;
  108. struct vmw_cmdbuf_header *cur;
  109. size_t cur_pos;
  110. size_t default_size;
  111. unsigned max_hw_submitted;
  112. spinlock_t lock;
  113. struct dma_pool *headers;
  114. struct dma_pool *dheaders;
  115. struct tasklet_struct tasklet;
  116. wait_queue_head_t alloc_queue;
  117. wait_queue_head_t idle_queue;
  118. bool irq_on;
  119. bool using_mob;
  120. bool has_pool;
  121. dma_addr_t handle;
  122. size_t size;
  123. };
  124. /**
  125. * struct vmw_cmdbuf_header - Command buffer metadata
  126. *
  127. * @man: The command buffer manager.
  128. * @cb_header: Device command buffer header, allocated from a DMA pool.
  129. * @cb_context: The device command buffer context.
  130. * @list: List head for attaching to the manager lists.
  131. * @node: The range manager node.
  132. * @handle. The DMA address of @cb_header. Handed to the device on command
  133. * buffer submission.
  134. * @cmd: Pointer to the command buffer space of this buffer.
  135. * @size: Size of the command buffer space of this buffer.
  136. * @reserved: Reserved space of this buffer.
  137. * @inline_space: Whether inline command buffer space is used.
  138. */
  139. struct vmw_cmdbuf_header {
  140. struct vmw_cmdbuf_man *man;
  141. SVGACBHeader *cb_header;
  142. SVGACBContext cb_context;
  143. struct list_head list;
  144. struct drm_mm_node node;
  145. dma_addr_t handle;
  146. u8 *cmd;
  147. size_t size;
  148. size_t reserved;
  149. bool inline_space;
  150. };
  151. /**
  152. * struct vmw_cmdbuf_dheader - Device command buffer header with inline
  153. * command buffer space.
  154. *
  155. * @cb_header: Device command buffer header.
  156. * @cmd: Inline command buffer space.
  157. */
  158. struct vmw_cmdbuf_dheader {
  159. SVGACBHeader cb_header;
  160. u8 cmd[VMW_CMDBUF_INLINE_SIZE] __aligned(VMW_CMDBUF_INLINE_ALIGN);
  161. };
  162. /**
  163. * struct vmw_cmdbuf_alloc_info - Command buffer space allocation metadata
  164. *
  165. * @page_size: Size of requested command buffer space in pages.
  166. * @node: Pointer to the range manager node.
  167. * @done: True if this allocation has succeeded.
  168. */
  169. struct vmw_cmdbuf_alloc_info {
  170. size_t page_size;
  171. struct drm_mm_node *node;
  172. bool done;
  173. };
  174. /* Loop over each context in the command buffer manager. */
  175. #define for_each_cmdbuf_ctx(_man, _i, _ctx) \
  176. for (_i = 0, _ctx = &(_man)->ctx[0]; (_i) < SVGA_CB_CONTEXT_MAX; \
  177. ++(_i), ++(_ctx))
  178. static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, bool enable);
  179. /**
  180. * vmw_cmdbuf_cur_lock - Helper to lock the cur_mutex.
  181. *
  182. * @man: The range manager.
  183. * @interruptible: Whether to wait interruptible when locking.
  184. */
  185. static int vmw_cmdbuf_cur_lock(struct vmw_cmdbuf_man *man, bool interruptible)
  186. {
  187. if (interruptible) {
  188. if (mutex_lock_interruptible(&man->cur_mutex))
  189. return -ERESTARTSYS;
  190. } else {
  191. mutex_lock(&man->cur_mutex);
  192. }
  193. return 0;
  194. }
  195. /**
  196. * vmw_cmdbuf_cur_unlock - Helper to unlock the cur_mutex.
  197. *
  198. * @man: The range manager.
  199. */
  200. static void vmw_cmdbuf_cur_unlock(struct vmw_cmdbuf_man *man)
  201. {
  202. mutex_unlock(&man->cur_mutex);
  203. }
  204. /**
  205. * vmw_cmdbuf_header_inline_free - Free a struct vmw_cmdbuf_header that has
  206. * been used for the device context with inline command buffers.
  207. * Need not be called locked.
  208. *
  209. * @header: Pointer to the header to free.
  210. */
  211. static void vmw_cmdbuf_header_inline_free(struct vmw_cmdbuf_header *header)
  212. {
  213. struct vmw_cmdbuf_dheader *dheader;
  214. if (WARN_ON_ONCE(!header->inline_space))
  215. return;
  216. dheader = container_of(header->cb_header, struct vmw_cmdbuf_dheader,
  217. cb_header);
  218. dma_pool_free(header->man->dheaders, dheader, header->handle);
  219. kfree(header);
  220. }
  221. /**
  222. * __vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
  223. * associated structures.
  224. *
  225. * header: Pointer to the header to free.
  226. *
  227. * For internal use. Must be called with man::lock held.
  228. */
  229. static void __vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
  230. {
  231. struct vmw_cmdbuf_man *man = header->man;
  232. BUG_ON(!spin_is_locked(&man->lock));
  233. if (header->inline_space) {
  234. vmw_cmdbuf_header_inline_free(header);
  235. return;
  236. }
  237. drm_mm_remove_node(&header->node);
  238. wake_up_all(&man->alloc_queue);
  239. if (header->cb_header)
  240. dma_pool_free(man->headers, header->cb_header,
  241. header->handle);
  242. kfree(header);
  243. }
  244. /**
  245. * vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
  246. * associated structures.
  247. *
  248. * @header: Pointer to the header to free.
  249. */
  250. void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
  251. {
  252. struct vmw_cmdbuf_man *man = header->man;
  253. /* Avoid locking if inline_space */
  254. if (header->inline_space) {
  255. vmw_cmdbuf_header_inline_free(header);
  256. return;
  257. }
  258. spin_lock_bh(&man->lock);
  259. __vmw_cmdbuf_header_free(header);
  260. spin_unlock_bh(&man->lock);
  261. }
  262. /**
  263. * vmw_cmbuf_header_submit: Submit a command buffer to hardware.
  264. *
  265. * @header: The header of the buffer to submit.
  266. */
  267. static int vmw_cmdbuf_header_submit(struct vmw_cmdbuf_header *header)
  268. {
  269. struct vmw_cmdbuf_man *man = header->man;
  270. u32 val;
  271. if (sizeof(header->handle) > 4)
  272. val = (header->handle >> 32);
  273. else
  274. val = 0;
  275. vmw_write(man->dev_priv, SVGA_REG_COMMAND_HIGH, val);
  276. val = (header->handle & 0xFFFFFFFFULL);
  277. val |= header->cb_context & SVGA_CB_CONTEXT_MASK;
  278. vmw_write(man->dev_priv, SVGA_REG_COMMAND_LOW, val);
  279. return header->cb_header->status;
  280. }
  281. /**
  282. * vmw_cmdbuf_ctx_init: Initialize a command buffer context.
  283. *
  284. * @ctx: The command buffer context to initialize
  285. */
  286. static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context *ctx)
  287. {
  288. INIT_LIST_HEAD(&ctx->hw_submitted);
  289. INIT_LIST_HEAD(&ctx->submitted);
  290. INIT_LIST_HEAD(&ctx->preempted);
  291. ctx->num_hw_submitted = 0;
  292. }
  293. /**
  294. * vmw_cmdbuf_ctx_submit: Submit command buffers from a command buffer
  295. * context.
  296. *
  297. * @man: The command buffer manager.
  298. * @ctx: The command buffer context.
  299. *
  300. * Submits command buffers to hardware until there are no more command
  301. * buffers to submit or the hardware can't handle more command buffers.
  302. */
  303. static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man *man,
  304. struct vmw_cmdbuf_context *ctx)
  305. {
  306. while (ctx->num_hw_submitted < man->max_hw_submitted &&
  307. !list_empty(&ctx->submitted)) {
  308. struct vmw_cmdbuf_header *entry;
  309. SVGACBStatus status;
  310. entry = list_first_entry(&ctx->submitted,
  311. struct vmw_cmdbuf_header,
  312. list);
  313. status = vmw_cmdbuf_header_submit(entry);
  314. /* This should never happen */
  315. if (WARN_ON_ONCE(status == SVGA_CB_STATUS_QUEUE_FULL)) {
  316. entry->cb_header->status = SVGA_CB_STATUS_NONE;
  317. break;
  318. }
  319. list_del(&entry->list);
  320. list_add_tail(&entry->list, &ctx->hw_submitted);
  321. ctx->num_hw_submitted++;
  322. }
  323. }
  324. /**
  325. * vmw_cmdbuf_ctx_submit: Process a command buffer context.
  326. *
  327. * @man: The command buffer manager.
  328. * @ctx: The command buffer context.
  329. *
  330. * Submit command buffers to hardware if possible, and process finished
  331. * buffers. Typically freeing them, but on preemption or error take
  332. * appropriate action. Wake up waiters if appropriate.
  333. */
  334. static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man *man,
  335. struct vmw_cmdbuf_context *ctx,
  336. int *notempty)
  337. {
  338. struct vmw_cmdbuf_header *entry, *next;
  339. vmw_cmdbuf_ctx_submit(man, ctx);
  340. list_for_each_entry_safe(entry, next, &ctx->hw_submitted, list) {
  341. SVGACBStatus status = entry->cb_header->status;
  342. if (status == SVGA_CB_STATUS_NONE)
  343. break;
  344. list_del(&entry->list);
  345. wake_up_all(&man->idle_queue);
  346. ctx->num_hw_submitted--;
  347. switch (status) {
  348. case SVGA_CB_STATUS_COMPLETED:
  349. __vmw_cmdbuf_header_free(entry);
  350. break;
  351. case SVGA_CB_STATUS_COMMAND_ERROR:
  352. case SVGA_CB_STATUS_CB_HEADER_ERROR:
  353. list_add_tail(&entry->list, &man->error);
  354. schedule_work(&man->work);
  355. break;
  356. case SVGA_CB_STATUS_PREEMPTED:
  357. list_add(&entry->list, &ctx->preempted);
  358. break;
  359. default:
  360. WARN_ONCE(true, "Undefined command buffer status.\n");
  361. __vmw_cmdbuf_header_free(entry);
  362. break;
  363. }
  364. }
  365. vmw_cmdbuf_ctx_submit(man, ctx);
  366. if (!list_empty(&ctx->submitted))
  367. (*notempty)++;
  368. }
  369. /**
  370. * vmw_cmdbuf_man_process - Process all command buffer contexts and
  371. * switch on and off irqs as appropriate.
  372. *
  373. * @man: The command buffer manager.
  374. *
  375. * Calls vmw_cmdbuf_ctx_process() on all contexts. If any context has
  376. * command buffers left that are not submitted to hardware, Make sure
  377. * IRQ handling is turned on. Otherwise, make sure it's turned off. This
  378. * function may return -EAGAIN to indicate it should be rerun due to
  379. * possibly missed IRQs if IRQs has just been turned on.
  380. */
  381. static int vmw_cmdbuf_man_process(struct vmw_cmdbuf_man *man)
  382. {
  383. int notempty = 0;
  384. struct vmw_cmdbuf_context *ctx;
  385. int i;
  386. for_each_cmdbuf_ctx(man, i, ctx)
  387. vmw_cmdbuf_ctx_process(man, ctx, &notempty);
  388. if (man->irq_on && !notempty) {
  389. vmw_generic_waiter_remove(man->dev_priv,
  390. SVGA_IRQFLAG_COMMAND_BUFFER,
  391. &man->dev_priv->cmdbuf_waiters);
  392. man->irq_on = false;
  393. } else if (!man->irq_on && notempty) {
  394. vmw_generic_waiter_add(man->dev_priv,
  395. SVGA_IRQFLAG_COMMAND_BUFFER,
  396. &man->dev_priv->cmdbuf_waiters);
  397. man->irq_on = true;
  398. /* Rerun in case we just missed an irq. */
  399. return -EAGAIN;
  400. }
  401. return 0;
  402. }
  403. /**
  404. * vmw_cmdbuf_ctx_add - Schedule a command buffer for submission on a
  405. * command buffer context
  406. *
  407. * @man: The command buffer manager.
  408. * @header: The header of the buffer to submit.
  409. * @cb_context: The command buffer context to use.
  410. *
  411. * This function adds @header to the "submitted" queue of the command
  412. * buffer context identified by @cb_context. It then calls the command buffer
  413. * manager processing to potentially submit the buffer to hardware.
  414. * @man->lock needs to be held when calling this function.
  415. */
  416. static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man *man,
  417. struct vmw_cmdbuf_header *header,
  418. SVGACBContext cb_context)
  419. {
  420. if (!(header->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT))
  421. header->cb_header->dxContext = 0;
  422. header->cb_context = cb_context;
  423. list_add_tail(&header->list, &man->ctx[cb_context].submitted);
  424. if (vmw_cmdbuf_man_process(man) == -EAGAIN)
  425. vmw_cmdbuf_man_process(man);
  426. }
  427. /**
  428. * vmw_cmdbuf_man_tasklet - The main part of the command buffer interrupt
  429. * handler implemented as a tasklet.
  430. *
  431. * @data: Tasklet closure. A pointer to the command buffer manager cast to
  432. * an unsigned long.
  433. *
  434. * The bottom half (tasklet) of the interrupt handler simply calls into the
  435. * command buffer processor to free finished buffers and submit any
  436. * queued buffers to hardware.
  437. */
  438. static void vmw_cmdbuf_man_tasklet(unsigned long data)
  439. {
  440. struct vmw_cmdbuf_man *man = (struct vmw_cmdbuf_man *) data;
  441. spin_lock(&man->lock);
  442. if (vmw_cmdbuf_man_process(man) == -EAGAIN)
  443. (void) vmw_cmdbuf_man_process(man);
  444. spin_unlock(&man->lock);
  445. }
  446. /**
  447. * vmw_cmdbuf_work_func - The deferred work function that handles
  448. * command buffer errors.
  449. *
  450. * @work: The work func closure argument.
  451. *
  452. * Restarting the command buffer context after an error requires process
  453. * context, so it is deferred to this work function.
  454. */
  455. static void vmw_cmdbuf_work_func(struct work_struct *work)
  456. {
  457. struct vmw_cmdbuf_man *man =
  458. container_of(work, struct vmw_cmdbuf_man, work);
  459. struct vmw_cmdbuf_header *entry, *next;
  460. bool restart = false;
  461. spin_lock_bh(&man->lock);
  462. list_for_each_entry_safe(entry, next, &man->error, list) {
  463. restart = true;
  464. DRM_ERROR("Command buffer error.\n");
  465. list_del(&entry->list);
  466. __vmw_cmdbuf_header_free(entry);
  467. wake_up_all(&man->idle_queue);
  468. }
  469. spin_unlock_bh(&man->lock);
  470. if (restart && vmw_cmdbuf_startstop(man, true))
  471. DRM_ERROR("Failed restarting command buffer context 0.\n");
  472. }
  473. /**
  474. * vmw_cmdbuf_man idle - Check whether the command buffer manager is idle.
  475. *
  476. * @man: The command buffer manager.
  477. * @check_preempted: Check also the preempted queue for pending command buffers.
  478. *
  479. */
  480. static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man *man,
  481. bool check_preempted)
  482. {
  483. struct vmw_cmdbuf_context *ctx;
  484. bool idle = false;
  485. int i;
  486. spin_lock_bh(&man->lock);
  487. vmw_cmdbuf_man_process(man);
  488. for_each_cmdbuf_ctx(man, i, ctx) {
  489. if (!list_empty(&ctx->submitted) ||
  490. !list_empty(&ctx->hw_submitted) ||
  491. (check_preempted && !list_empty(&ctx->preempted)))
  492. goto out_unlock;
  493. }
  494. idle = list_empty(&man->error);
  495. out_unlock:
  496. spin_unlock_bh(&man->lock);
  497. return idle;
  498. }
  499. /**
  500. * __vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
  501. * command submissions
  502. *
  503. * @man: The command buffer manager.
  504. *
  505. * Flushes the current command buffer without allocating a new one. A new one
  506. * is automatically allocated when needed. Call with @man->cur_mutex held.
  507. */
  508. static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man)
  509. {
  510. struct vmw_cmdbuf_header *cur = man->cur;
  511. WARN_ON(!mutex_is_locked(&man->cur_mutex));
  512. if (!cur)
  513. return;
  514. spin_lock_bh(&man->lock);
  515. if (man->cur_pos == 0) {
  516. __vmw_cmdbuf_header_free(cur);
  517. goto out_unlock;
  518. }
  519. man->cur->cb_header->length = man->cur_pos;
  520. vmw_cmdbuf_ctx_add(man, man->cur, SVGA_CB_CONTEXT_0);
  521. out_unlock:
  522. spin_unlock_bh(&man->lock);
  523. man->cur = NULL;
  524. man->cur_pos = 0;
  525. }
  526. /**
  527. * vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
  528. * command submissions
  529. *
  530. * @man: The command buffer manager.
  531. * @interruptible: Whether to sleep interruptible when sleeping.
  532. *
  533. * Flushes the current command buffer without allocating a new one. A new one
  534. * is automatically allocated when needed.
  535. */
  536. int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man,
  537. bool interruptible)
  538. {
  539. int ret = vmw_cmdbuf_cur_lock(man, interruptible);
  540. if (ret)
  541. return ret;
  542. __vmw_cmdbuf_cur_flush(man);
  543. vmw_cmdbuf_cur_unlock(man);
  544. return 0;
  545. }
  546. /**
  547. * vmw_cmdbuf_idle - Wait for command buffer manager idle.
  548. *
  549. * @man: The command buffer manager.
  550. * @interruptible: Sleep interruptible while waiting.
  551. * @timeout: Time out after this many ticks.
  552. *
  553. * Wait until the command buffer manager has processed all command buffers,
  554. * or until a timeout occurs. If a timeout occurs, the function will return
  555. * -EBUSY.
  556. */
  557. int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible,
  558. unsigned long timeout)
  559. {
  560. int ret;
  561. ret = vmw_cmdbuf_cur_flush(man, interruptible);
  562. vmw_generic_waiter_add(man->dev_priv,
  563. SVGA_IRQFLAG_COMMAND_BUFFER,
  564. &man->dev_priv->cmdbuf_waiters);
  565. if (interruptible) {
  566. ret = wait_event_interruptible_timeout
  567. (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
  568. timeout);
  569. } else {
  570. ret = wait_event_timeout
  571. (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
  572. timeout);
  573. }
  574. vmw_generic_waiter_remove(man->dev_priv,
  575. SVGA_IRQFLAG_COMMAND_BUFFER,
  576. &man->dev_priv->cmdbuf_waiters);
  577. if (ret == 0) {
  578. if (!vmw_cmdbuf_man_idle(man, true))
  579. ret = -EBUSY;
  580. else
  581. ret = 0;
  582. }
  583. if (ret > 0)
  584. ret = 0;
  585. return ret;
  586. }
  587. /**
  588. * vmw_cmdbuf_try_alloc - Try to allocate buffer space from the main pool.
  589. *
  590. * @man: The command buffer manager.
  591. * @info: Allocation info. Will hold the size on entry and allocated mm node
  592. * on successful return.
  593. *
  594. * Try to allocate buffer space from the main pool. Returns true if succeeded.
  595. * If a fatal error was hit, the error code is returned in @info->ret.
  596. */
  597. static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man *man,
  598. struct vmw_cmdbuf_alloc_info *info)
  599. {
  600. int ret;
  601. if (info->done)
  602. return true;
  603. memset(info->node, 0, sizeof(*info->node));
  604. spin_lock_bh(&man->lock);
  605. ret = drm_mm_insert_node_generic(&man->mm, info->node, info->page_size,
  606. 0, 0,
  607. DRM_MM_SEARCH_DEFAULT,
  608. DRM_MM_CREATE_DEFAULT);
  609. spin_unlock_bh(&man->lock);
  610. info->done = !ret;
  611. return info->done;
  612. }
  613. /**
  614. * vmw_cmdbuf_alloc_space - Allocate buffer space from the main pool.
  615. *
  616. * @man: The command buffer manager.
  617. * @node: Pointer to pre-allocated range-manager node.
  618. * @size: The size of the allocation.
  619. * @interruptible: Whether to sleep interruptible while waiting for space.
  620. *
  621. * This function allocates buffer space from the main pool, and if there is
  622. * no space available ATM, it turns on IRQ handling and sleeps waiting for it to
  623. * become available.
  624. */
  625. static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man *man,
  626. struct drm_mm_node *node,
  627. size_t size,
  628. bool interruptible)
  629. {
  630. struct vmw_cmdbuf_alloc_info info;
  631. info.page_size = PAGE_ALIGN(size) >> PAGE_SHIFT;
  632. info.node = node;
  633. info.done = false;
  634. /*
  635. * To prevent starvation of large requests, only one allocating call
  636. * at a time waiting for space.
  637. */
  638. if (interruptible) {
  639. if (mutex_lock_interruptible(&man->space_mutex))
  640. return -ERESTARTSYS;
  641. } else {
  642. mutex_lock(&man->space_mutex);
  643. }
  644. /* Try to allocate space without waiting. */
  645. if (vmw_cmdbuf_try_alloc(man, &info))
  646. goto out_unlock;
  647. vmw_generic_waiter_add(man->dev_priv,
  648. SVGA_IRQFLAG_COMMAND_BUFFER,
  649. &man->dev_priv->cmdbuf_waiters);
  650. if (interruptible) {
  651. int ret;
  652. ret = wait_event_interruptible
  653. (man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
  654. if (ret) {
  655. vmw_generic_waiter_remove
  656. (man->dev_priv, SVGA_IRQFLAG_COMMAND_BUFFER,
  657. &man->dev_priv->cmdbuf_waiters);
  658. mutex_unlock(&man->space_mutex);
  659. return ret;
  660. }
  661. } else {
  662. wait_event(man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
  663. }
  664. vmw_generic_waiter_remove(man->dev_priv,
  665. SVGA_IRQFLAG_COMMAND_BUFFER,
  666. &man->dev_priv->cmdbuf_waiters);
  667. out_unlock:
  668. mutex_unlock(&man->space_mutex);
  669. return 0;
  670. }
  671. /**
  672. * vmw_cmdbuf_space_pool - Set up a command buffer header with command buffer
  673. * space from the main pool.
  674. *
  675. * @man: The command buffer manager.
  676. * @header: Pointer to the header to set up.
  677. * @size: The requested size of the buffer space.
  678. * @interruptible: Whether to sleep interruptible while waiting for space.
  679. */
  680. static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man *man,
  681. struct vmw_cmdbuf_header *header,
  682. size_t size,
  683. bool interruptible)
  684. {
  685. SVGACBHeader *cb_hdr;
  686. size_t offset;
  687. int ret;
  688. if (!man->has_pool)
  689. return -ENOMEM;
  690. ret = vmw_cmdbuf_alloc_space(man, &header->node, size, interruptible);
  691. if (ret)
  692. return ret;
  693. header->cb_header = dma_pool_alloc(man->headers, GFP_KERNEL,
  694. &header->handle);
  695. if (!header->cb_header) {
  696. ret = -ENOMEM;
  697. goto out_no_cb_header;
  698. }
  699. header->size = header->node.size << PAGE_SHIFT;
  700. cb_hdr = header->cb_header;
  701. offset = header->node.start << PAGE_SHIFT;
  702. header->cmd = man->map + offset;
  703. memset(cb_hdr, 0, sizeof(*cb_hdr));
  704. if (man->using_mob) {
  705. cb_hdr->flags = SVGA_CB_FLAG_MOB;
  706. cb_hdr->ptr.mob.mobid = man->cmd_space->mem.start;
  707. cb_hdr->ptr.mob.mobOffset = offset;
  708. } else {
  709. cb_hdr->ptr.pa = (u64)man->handle + (u64)offset;
  710. }
  711. return 0;
  712. out_no_cb_header:
  713. spin_lock_bh(&man->lock);
  714. drm_mm_remove_node(&header->node);
  715. spin_unlock_bh(&man->lock);
  716. return ret;
  717. }
  718. /**
  719. * vmw_cmdbuf_space_inline - Set up a command buffer header with
  720. * inline command buffer space.
  721. *
  722. * @man: The command buffer manager.
  723. * @header: Pointer to the header to set up.
  724. * @size: The requested size of the buffer space.
  725. */
  726. static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man *man,
  727. struct vmw_cmdbuf_header *header,
  728. int size)
  729. {
  730. struct vmw_cmdbuf_dheader *dheader;
  731. SVGACBHeader *cb_hdr;
  732. if (WARN_ON_ONCE(size > VMW_CMDBUF_INLINE_SIZE))
  733. return -ENOMEM;
  734. dheader = dma_pool_alloc(man->dheaders, GFP_KERNEL,
  735. &header->handle);
  736. if (!dheader)
  737. return -ENOMEM;
  738. header->inline_space = true;
  739. header->size = VMW_CMDBUF_INLINE_SIZE;
  740. cb_hdr = &dheader->cb_header;
  741. header->cb_header = cb_hdr;
  742. header->cmd = dheader->cmd;
  743. memset(dheader, 0, sizeof(*dheader));
  744. cb_hdr->status = SVGA_CB_STATUS_NONE;
  745. cb_hdr->flags = SVGA_CB_FLAG_NONE;
  746. cb_hdr->ptr.pa = (u64)header->handle +
  747. (u64)offsetof(struct vmw_cmdbuf_dheader, cmd);
  748. return 0;
  749. }
  750. /**
  751. * vmw_cmdbuf_alloc - Allocate a command buffer header complete with
  752. * command buffer space.
  753. *
  754. * @man: The command buffer manager.
  755. * @size: The requested size of the buffer space.
  756. * @interruptible: Whether to sleep interruptible while waiting for space.
  757. * @p_header: points to a header pointer to populate on successful return.
  758. *
  759. * Returns a pointer to command buffer space if successful. Otherwise
  760. * returns an error pointer. The header pointer returned in @p_header should
  761. * be used for upcoming calls to vmw_cmdbuf_reserve() and vmw_cmdbuf_commit().
  762. */
  763. void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man,
  764. size_t size, bool interruptible,
  765. struct vmw_cmdbuf_header **p_header)
  766. {
  767. struct vmw_cmdbuf_header *header;
  768. int ret = 0;
  769. *p_header = NULL;
  770. header = kzalloc(sizeof(*header), GFP_KERNEL);
  771. if (!header)
  772. return ERR_PTR(-ENOMEM);
  773. if (size <= VMW_CMDBUF_INLINE_SIZE)
  774. ret = vmw_cmdbuf_space_inline(man, header, size);
  775. else
  776. ret = vmw_cmdbuf_space_pool(man, header, size, interruptible);
  777. if (ret) {
  778. kfree(header);
  779. return ERR_PTR(ret);
  780. }
  781. header->man = man;
  782. INIT_LIST_HEAD(&header->list);
  783. header->cb_header->status = SVGA_CB_STATUS_NONE;
  784. *p_header = header;
  785. return header->cmd;
  786. }
  787. /**
  788. * vmw_cmdbuf_reserve_cur - Reserve space for commands in the current
  789. * command buffer.
  790. *
  791. * @man: The command buffer manager.
  792. * @size: The requested size of the commands.
  793. * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
  794. * @interruptible: Whether to sleep interruptible while waiting for space.
  795. *
  796. * Returns a pointer to command buffer space if successful. Otherwise
  797. * returns an error pointer.
  798. */
  799. static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man *man,
  800. size_t size,
  801. int ctx_id,
  802. bool interruptible)
  803. {
  804. struct vmw_cmdbuf_header *cur;
  805. void *ret;
  806. if (vmw_cmdbuf_cur_lock(man, interruptible))
  807. return ERR_PTR(-ERESTARTSYS);
  808. cur = man->cur;
  809. if (cur && (size + man->cur_pos > cur->size ||
  810. ((cur->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT) &&
  811. ctx_id != cur->cb_header->dxContext)))
  812. __vmw_cmdbuf_cur_flush(man);
  813. if (!man->cur) {
  814. ret = vmw_cmdbuf_alloc(man,
  815. max_t(size_t, size, man->default_size),
  816. interruptible, &man->cur);
  817. if (IS_ERR(ret)) {
  818. vmw_cmdbuf_cur_unlock(man);
  819. return ret;
  820. }
  821. cur = man->cur;
  822. }
  823. if (ctx_id != SVGA3D_INVALID_ID) {
  824. cur->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  825. cur->cb_header->dxContext = ctx_id;
  826. }
  827. cur->reserved = size;
  828. return (void *) (man->cur->cmd + man->cur_pos);
  829. }
  830. /**
  831. * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer.
  832. *
  833. * @man: The command buffer manager.
  834. * @size: The size of the commands actually written.
  835. * @flush: Whether to flush the command buffer immediately.
  836. */
  837. static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man *man,
  838. size_t size, bool flush)
  839. {
  840. struct vmw_cmdbuf_header *cur = man->cur;
  841. WARN_ON(!mutex_is_locked(&man->cur_mutex));
  842. WARN_ON(size > cur->reserved);
  843. man->cur_pos += size;
  844. if (!size)
  845. cur->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  846. if (flush)
  847. __vmw_cmdbuf_cur_flush(man);
  848. vmw_cmdbuf_cur_unlock(man);
  849. }
  850. /**
  851. * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer.
  852. *
  853. * @man: The command buffer manager.
  854. * @size: The requested size of the commands.
  855. * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
  856. * @interruptible: Whether to sleep interruptible while waiting for space.
  857. * @header: Header of the command buffer. NULL if the current command buffer
  858. * should be used.
  859. *
  860. * Returns a pointer to command buffer space if successful. Otherwise
  861. * returns an error pointer.
  862. */
  863. void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size,
  864. int ctx_id, bool interruptible,
  865. struct vmw_cmdbuf_header *header)
  866. {
  867. if (!header)
  868. return vmw_cmdbuf_reserve_cur(man, size, ctx_id, interruptible);
  869. if (size > header->size)
  870. return ERR_PTR(-EINVAL);
  871. if (ctx_id != SVGA3D_INVALID_ID) {
  872. header->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  873. header->cb_header->dxContext = ctx_id;
  874. }
  875. header->reserved = size;
  876. return header->cmd;
  877. }
  878. /**
  879. * vmw_cmdbuf_commit - Commit commands in a command buffer.
  880. *
  881. * @man: The command buffer manager.
  882. * @size: The size of the commands actually written.
  883. * @header: Header of the command buffer. NULL if the current command buffer
  884. * should be used.
  885. * @flush: Whether to flush the command buffer immediately.
  886. */
  887. void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size,
  888. struct vmw_cmdbuf_header *header, bool flush)
  889. {
  890. if (!header) {
  891. vmw_cmdbuf_commit_cur(man, size, flush);
  892. return;
  893. }
  894. (void) vmw_cmdbuf_cur_lock(man, false);
  895. __vmw_cmdbuf_cur_flush(man);
  896. WARN_ON(size > header->reserved);
  897. man->cur = header;
  898. man->cur_pos = size;
  899. if (!size)
  900. header->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  901. if (flush)
  902. __vmw_cmdbuf_cur_flush(man);
  903. vmw_cmdbuf_cur_unlock(man);
  904. }
  905. /**
  906. * vmw_cmdbuf_tasklet_schedule - Schedule the interrupt handler bottom half.
  907. *
  908. * @man: The command buffer manager.
  909. */
  910. void vmw_cmdbuf_tasklet_schedule(struct vmw_cmdbuf_man *man)
  911. {
  912. if (!man)
  913. return;
  914. tasklet_schedule(&man->tasklet);
  915. }
  916. /**
  917. * vmw_cmdbuf_send_device_command - Send a command through the device context.
  918. *
  919. * @man: The command buffer manager.
  920. * @command: Pointer to the command to send.
  921. * @size: Size of the command.
  922. *
  923. * Synchronously sends a device context command.
  924. */
  925. static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man *man,
  926. const void *command,
  927. size_t size)
  928. {
  929. struct vmw_cmdbuf_header *header;
  930. int status;
  931. void *cmd = vmw_cmdbuf_alloc(man, size, false, &header);
  932. if (IS_ERR(cmd))
  933. return PTR_ERR(cmd);
  934. memcpy(cmd, command, size);
  935. header->cb_header->length = size;
  936. header->cb_context = SVGA_CB_CONTEXT_DEVICE;
  937. spin_lock_bh(&man->lock);
  938. status = vmw_cmdbuf_header_submit(header);
  939. spin_unlock_bh(&man->lock);
  940. vmw_cmdbuf_header_free(header);
  941. if (status != SVGA_CB_STATUS_COMPLETED) {
  942. DRM_ERROR("Device context command failed with status %d\n",
  943. status);
  944. return -EINVAL;
  945. }
  946. return 0;
  947. }
  948. /**
  949. * vmw_cmdbuf_startstop - Send a start / stop command through the device
  950. * context.
  951. *
  952. * @man: The command buffer manager.
  953. * @enable: Whether to enable or disable the context.
  954. *
  955. * Synchronously sends a device start / stop context command.
  956. */
  957. static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man,
  958. bool enable)
  959. {
  960. struct {
  961. uint32 id;
  962. SVGADCCmdStartStop body;
  963. } __packed cmd;
  964. cmd.id = SVGA_DC_CMD_START_STOP_CONTEXT;
  965. cmd.body.enable = (enable) ? 1 : 0;
  966. cmd.body.context = SVGA_CB_CONTEXT_0;
  967. return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd));
  968. }
  969. /**
  970. * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes
  971. *
  972. * @man: The command buffer manager.
  973. * @size: The size of the main space pool.
  974. * @default_size: The default size of the command buffer for small kernel
  975. * submissions.
  976. *
  977. * Set the size and allocate the main command buffer space pool,
  978. * as well as the default size of the command buffer for
  979. * small kernel submissions. If successful, this enables large command
  980. * submissions. Note that this function requires that rudimentary command
  981. * submission is already available and that the MOB memory manager is alive.
  982. * Returns 0 on success. Negative error code on failure.
  983. */
  984. int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man,
  985. size_t size, size_t default_size)
  986. {
  987. struct vmw_private *dev_priv = man->dev_priv;
  988. bool dummy;
  989. int ret;
  990. if (man->has_pool)
  991. return -EINVAL;
  992. /* First, try to allocate a huge chunk of DMA memory */
  993. size = PAGE_ALIGN(size);
  994. man->map = dma_alloc_coherent(&dev_priv->dev->pdev->dev, size,
  995. &man->handle, GFP_KERNEL);
  996. if (man->map) {
  997. man->using_mob = false;
  998. } else {
  999. /*
  1000. * DMA memory failed. If we can have command buffers in a
  1001. * MOB, try to use that instead. Note that this will
  1002. * actually call into the already enabled manager, when
  1003. * binding the MOB.
  1004. */
  1005. if (!(dev_priv->capabilities & SVGA_CAP_DX))
  1006. return -ENOMEM;
  1007. ret = ttm_bo_create(&dev_priv->bdev, size, ttm_bo_type_device,
  1008. &vmw_mob_ne_placement, 0, false, NULL,
  1009. &man->cmd_space);
  1010. if (ret)
  1011. return ret;
  1012. man->using_mob = true;
  1013. ret = ttm_bo_kmap(man->cmd_space, 0, size >> PAGE_SHIFT,
  1014. &man->map_obj);
  1015. if (ret)
  1016. goto out_no_map;
  1017. man->map = ttm_kmap_obj_virtual(&man->map_obj, &dummy);
  1018. }
  1019. man->size = size;
  1020. drm_mm_init(&man->mm, 0, size >> PAGE_SHIFT);
  1021. man->has_pool = true;
  1022. man->default_size = default_size;
  1023. DRM_INFO("Using command buffers with %s pool.\n",
  1024. (man->using_mob) ? "MOB" : "DMA");
  1025. return 0;
  1026. out_no_map:
  1027. if (man->using_mob)
  1028. ttm_bo_unref(&man->cmd_space);
  1029. return ret;
  1030. }
  1031. /**
  1032. * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for
  1033. * inline command buffer submissions only.
  1034. *
  1035. * @dev_priv: Pointer to device private structure.
  1036. *
  1037. * Returns a pointer to a cummand buffer manager to success or error pointer
  1038. * on failure. The command buffer manager will be enabled for submissions of
  1039. * size VMW_CMDBUF_INLINE_SIZE only.
  1040. */
  1041. struct vmw_cmdbuf_man *vmw_cmdbuf_man_create(struct vmw_private *dev_priv)
  1042. {
  1043. struct vmw_cmdbuf_man *man;
  1044. struct vmw_cmdbuf_context *ctx;
  1045. int i;
  1046. int ret;
  1047. if (!(dev_priv->capabilities & SVGA_CAP_COMMAND_BUFFERS))
  1048. return ERR_PTR(-ENOSYS);
  1049. man = kzalloc(sizeof(*man), GFP_KERNEL);
  1050. if (!man)
  1051. return ERR_PTR(-ENOMEM);
  1052. man->headers = dma_pool_create("vmwgfx cmdbuf",
  1053. &dev_priv->dev->pdev->dev,
  1054. sizeof(SVGACBHeader),
  1055. 64, PAGE_SIZE);
  1056. if (!man->headers) {
  1057. ret = -ENOMEM;
  1058. goto out_no_pool;
  1059. }
  1060. man->dheaders = dma_pool_create("vmwgfx inline cmdbuf",
  1061. &dev_priv->dev->pdev->dev,
  1062. sizeof(struct vmw_cmdbuf_dheader),
  1063. 64, PAGE_SIZE);
  1064. if (!man->dheaders) {
  1065. ret = -ENOMEM;
  1066. goto out_no_dpool;
  1067. }
  1068. for_each_cmdbuf_ctx(man, i, ctx)
  1069. vmw_cmdbuf_ctx_init(ctx);
  1070. INIT_LIST_HEAD(&man->error);
  1071. spin_lock_init(&man->lock);
  1072. mutex_init(&man->cur_mutex);
  1073. mutex_init(&man->space_mutex);
  1074. tasklet_init(&man->tasklet, vmw_cmdbuf_man_tasklet,
  1075. (unsigned long) man);
  1076. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1077. init_waitqueue_head(&man->alloc_queue);
  1078. init_waitqueue_head(&man->idle_queue);
  1079. man->dev_priv = dev_priv;
  1080. man->max_hw_submitted = SVGA_CB_MAX_QUEUED_PER_CONTEXT - 1;
  1081. INIT_WORK(&man->work, &vmw_cmdbuf_work_func);
  1082. vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ERROR,
  1083. &dev_priv->error_waiters);
  1084. ret = vmw_cmdbuf_startstop(man, true);
  1085. if (ret) {
  1086. DRM_ERROR("Failed starting command buffer context 0.\n");
  1087. vmw_cmdbuf_man_destroy(man);
  1088. return ERR_PTR(ret);
  1089. }
  1090. return man;
  1091. out_no_dpool:
  1092. dma_pool_destroy(man->headers);
  1093. out_no_pool:
  1094. kfree(man);
  1095. return ERR_PTR(ret);
  1096. }
  1097. /**
  1098. * vmw_cmdbuf_remove_pool - Take down the main buffer space pool.
  1099. *
  1100. * @man: Pointer to a command buffer manager.
  1101. *
  1102. * This function removes the main buffer space pool, and should be called
  1103. * before MOB memory management is removed. When this function has been called,
  1104. * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or
  1105. * less are allowed, and the default size of the command buffer for small kernel
  1106. * submissions is also set to this size.
  1107. */
  1108. void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man)
  1109. {
  1110. if (!man->has_pool)
  1111. return;
  1112. man->has_pool = false;
  1113. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1114. (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1115. if (man->using_mob) {
  1116. (void) ttm_bo_kunmap(&man->map_obj);
  1117. ttm_bo_unref(&man->cmd_space);
  1118. } else {
  1119. dma_free_coherent(&man->dev_priv->dev->pdev->dev,
  1120. man->size, man->map, man->handle);
  1121. }
  1122. }
  1123. /**
  1124. * vmw_cmdbuf_man_destroy - Take down a command buffer manager.
  1125. *
  1126. * @man: Pointer to a command buffer manager.
  1127. *
  1128. * This function idles and then destroys a command buffer manager.
  1129. */
  1130. void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man)
  1131. {
  1132. WARN_ON_ONCE(man->has_pool);
  1133. (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1134. if (vmw_cmdbuf_startstop(man, false))
  1135. DRM_ERROR("Failed stopping command buffer context 0.\n");
  1136. vmw_generic_waiter_remove(man->dev_priv, SVGA_IRQFLAG_ERROR,
  1137. &man->dev_priv->error_waiters);
  1138. tasklet_kill(&man->tasklet);
  1139. (void) cancel_work_sync(&man->work);
  1140. dma_pool_destroy(man->dheaders);
  1141. dma_pool_destroy(man->headers);
  1142. mutex_destroy(&man->cur_mutex);
  1143. mutex_destroy(&man->space_mutex);
  1144. kfree(man);
  1145. }