vmwgfx_cmdbuf.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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. val = (header->handle >> 32);
  272. vmw_write(man->dev_priv, SVGA_REG_COMMAND_HIGH, val);
  273. val = (header->handle & 0xFFFFFFFFULL);
  274. val |= header->cb_context & SVGA_CB_CONTEXT_MASK;
  275. vmw_write(man->dev_priv, SVGA_REG_COMMAND_LOW, val);
  276. return header->cb_header->status;
  277. }
  278. /**
  279. * vmw_cmdbuf_ctx_init: Initialize a command buffer context.
  280. *
  281. * @ctx: The command buffer context to initialize
  282. */
  283. static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context *ctx)
  284. {
  285. INIT_LIST_HEAD(&ctx->hw_submitted);
  286. INIT_LIST_HEAD(&ctx->submitted);
  287. INIT_LIST_HEAD(&ctx->preempted);
  288. ctx->num_hw_submitted = 0;
  289. }
  290. /**
  291. * vmw_cmdbuf_ctx_submit: Submit command buffers from a command buffer
  292. * context.
  293. *
  294. * @man: The command buffer manager.
  295. * @ctx: The command buffer context.
  296. *
  297. * Submits command buffers to hardware until there are no more command
  298. * buffers to submit or the hardware can't handle more command buffers.
  299. */
  300. static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man *man,
  301. struct vmw_cmdbuf_context *ctx)
  302. {
  303. while (ctx->num_hw_submitted < man->max_hw_submitted &&
  304. !list_empty(&ctx->submitted)) {
  305. struct vmw_cmdbuf_header *entry;
  306. SVGACBStatus status;
  307. entry = list_first_entry(&ctx->submitted,
  308. struct vmw_cmdbuf_header,
  309. list);
  310. status = vmw_cmdbuf_header_submit(entry);
  311. /* This should never happen */
  312. if (WARN_ON_ONCE(status == SVGA_CB_STATUS_QUEUE_FULL)) {
  313. entry->cb_header->status = SVGA_CB_STATUS_NONE;
  314. break;
  315. }
  316. list_del(&entry->list);
  317. list_add_tail(&entry->list, &ctx->hw_submitted);
  318. ctx->num_hw_submitted++;
  319. }
  320. }
  321. /**
  322. * vmw_cmdbuf_ctx_submit: Process a command buffer context.
  323. *
  324. * @man: The command buffer manager.
  325. * @ctx: The command buffer context.
  326. *
  327. * Submit command buffers to hardware if possible, and process finished
  328. * buffers. Typically freeing them, but on preemption or error take
  329. * appropriate action. Wake up waiters if appropriate.
  330. */
  331. static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man *man,
  332. struct vmw_cmdbuf_context *ctx,
  333. int *notempty)
  334. {
  335. struct vmw_cmdbuf_header *entry, *next;
  336. vmw_cmdbuf_ctx_submit(man, ctx);
  337. list_for_each_entry_safe(entry, next, &ctx->hw_submitted, list) {
  338. SVGACBStatus status = entry->cb_header->status;
  339. if (status == SVGA_CB_STATUS_NONE)
  340. break;
  341. list_del(&entry->list);
  342. wake_up_all(&man->idle_queue);
  343. ctx->num_hw_submitted--;
  344. switch (status) {
  345. case SVGA_CB_STATUS_COMPLETED:
  346. __vmw_cmdbuf_header_free(entry);
  347. break;
  348. case SVGA_CB_STATUS_COMMAND_ERROR:
  349. case SVGA_CB_STATUS_CB_HEADER_ERROR:
  350. list_add_tail(&entry->list, &man->error);
  351. schedule_work(&man->work);
  352. break;
  353. case SVGA_CB_STATUS_PREEMPTED:
  354. list_add(&entry->list, &ctx->preempted);
  355. break;
  356. default:
  357. WARN_ONCE(true, "Undefined command buffer status.\n");
  358. __vmw_cmdbuf_header_free(entry);
  359. break;
  360. }
  361. }
  362. vmw_cmdbuf_ctx_submit(man, ctx);
  363. if (!list_empty(&ctx->submitted))
  364. (*notempty)++;
  365. }
  366. /**
  367. * vmw_cmdbuf_man_process - Process all command buffer contexts and
  368. * switch on and off irqs as appropriate.
  369. *
  370. * @man: The command buffer manager.
  371. *
  372. * Calls vmw_cmdbuf_ctx_process() on all contexts. If any context has
  373. * command buffers left that are not submitted to hardware, Make sure
  374. * IRQ handling is turned on. Otherwise, make sure it's turned off. This
  375. * function may return -EAGAIN to indicate it should be rerun due to
  376. * possibly missed IRQs if IRQs has just been turned on.
  377. */
  378. static int vmw_cmdbuf_man_process(struct vmw_cmdbuf_man *man)
  379. {
  380. int notempty = 0;
  381. struct vmw_cmdbuf_context *ctx;
  382. int i;
  383. for_each_cmdbuf_ctx(man, i, ctx)
  384. vmw_cmdbuf_ctx_process(man, ctx, &notempty);
  385. if (man->irq_on && !notempty) {
  386. vmw_generic_waiter_remove(man->dev_priv,
  387. SVGA_IRQFLAG_COMMAND_BUFFER,
  388. &man->dev_priv->cmdbuf_waiters);
  389. man->irq_on = false;
  390. } else if (!man->irq_on && notempty) {
  391. vmw_generic_waiter_add(man->dev_priv,
  392. SVGA_IRQFLAG_COMMAND_BUFFER,
  393. &man->dev_priv->cmdbuf_waiters);
  394. man->irq_on = true;
  395. /* Rerun in case we just missed an irq. */
  396. return -EAGAIN;
  397. }
  398. return 0;
  399. }
  400. /**
  401. * vmw_cmdbuf_ctx_add - Schedule a command buffer for submission on a
  402. * command buffer context
  403. *
  404. * @man: The command buffer manager.
  405. * @header: The header of the buffer to submit.
  406. * @cb_context: The command buffer context to use.
  407. *
  408. * This function adds @header to the "submitted" queue of the command
  409. * buffer context identified by @cb_context. It then calls the command buffer
  410. * manager processing to potentially submit the buffer to hardware.
  411. * @man->lock needs to be held when calling this function.
  412. */
  413. static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man *man,
  414. struct vmw_cmdbuf_header *header,
  415. SVGACBContext cb_context)
  416. {
  417. if (!(header->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT))
  418. header->cb_header->dxContext = 0;
  419. header->cb_context = cb_context;
  420. list_add_tail(&header->list, &man->ctx[cb_context].submitted);
  421. if (vmw_cmdbuf_man_process(man) == -EAGAIN)
  422. vmw_cmdbuf_man_process(man);
  423. }
  424. /**
  425. * vmw_cmdbuf_man_tasklet - The main part of the command buffer interrupt
  426. * handler implemented as a tasklet.
  427. *
  428. * @data: Tasklet closure. A pointer to the command buffer manager cast to
  429. * an unsigned long.
  430. *
  431. * The bottom half (tasklet) of the interrupt handler simply calls into the
  432. * command buffer processor to free finished buffers and submit any
  433. * queued buffers to hardware.
  434. */
  435. static void vmw_cmdbuf_man_tasklet(unsigned long data)
  436. {
  437. struct vmw_cmdbuf_man *man = (struct vmw_cmdbuf_man *) data;
  438. spin_lock(&man->lock);
  439. if (vmw_cmdbuf_man_process(man) == -EAGAIN)
  440. (void) vmw_cmdbuf_man_process(man);
  441. spin_unlock(&man->lock);
  442. }
  443. /**
  444. * vmw_cmdbuf_work_func - The deferred work function that handles
  445. * command buffer errors.
  446. *
  447. * @work: The work func closure argument.
  448. *
  449. * Restarting the command buffer context after an error requires process
  450. * context, so it is deferred to this work function.
  451. */
  452. static void vmw_cmdbuf_work_func(struct work_struct *work)
  453. {
  454. struct vmw_cmdbuf_man *man =
  455. container_of(work, struct vmw_cmdbuf_man, work);
  456. struct vmw_cmdbuf_header *entry, *next;
  457. bool restart;
  458. spin_lock_bh(&man->lock);
  459. list_for_each_entry_safe(entry, next, &man->error, list) {
  460. restart = true;
  461. DRM_ERROR("Command buffer error.\n");
  462. list_del(&entry->list);
  463. __vmw_cmdbuf_header_free(entry);
  464. wake_up_all(&man->idle_queue);
  465. }
  466. spin_unlock_bh(&man->lock);
  467. if (restart && vmw_cmdbuf_startstop(man, true))
  468. DRM_ERROR("Failed restarting command buffer context 0.\n");
  469. }
  470. /**
  471. * vmw_cmdbuf_man idle - Check whether the command buffer manager is idle.
  472. *
  473. * @man: The command buffer manager.
  474. * @check_preempted: Check also the preempted queue for pending command buffers.
  475. *
  476. */
  477. static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man *man,
  478. bool check_preempted)
  479. {
  480. struct vmw_cmdbuf_context *ctx;
  481. bool idle = false;
  482. int i;
  483. spin_lock_bh(&man->lock);
  484. vmw_cmdbuf_man_process(man);
  485. for_each_cmdbuf_ctx(man, i, ctx) {
  486. if (!list_empty(&ctx->submitted) ||
  487. !list_empty(&ctx->hw_submitted) ||
  488. (check_preempted && !list_empty(&ctx->preempted)))
  489. goto out_unlock;
  490. }
  491. idle = list_empty(&man->error);
  492. out_unlock:
  493. spin_unlock_bh(&man->lock);
  494. return idle;
  495. }
  496. /**
  497. * __vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
  498. * command submissions
  499. *
  500. * @man: The command buffer manager.
  501. *
  502. * Flushes the current command buffer without allocating a new one. A new one
  503. * is automatically allocated when needed. Call with @man->cur_mutex held.
  504. */
  505. static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man)
  506. {
  507. struct vmw_cmdbuf_header *cur = man->cur;
  508. WARN_ON(!mutex_is_locked(&man->cur_mutex));
  509. if (!cur)
  510. return;
  511. spin_lock_bh(&man->lock);
  512. if (man->cur_pos == 0) {
  513. __vmw_cmdbuf_header_free(cur);
  514. goto out_unlock;
  515. }
  516. man->cur->cb_header->length = man->cur_pos;
  517. vmw_cmdbuf_ctx_add(man, man->cur, SVGA_CB_CONTEXT_0);
  518. out_unlock:
  519. spin_unlock_bh(&man->lock);
  520. man->cur = NULL;
  521. man->cur_pos = 0;
  522. }
  523. /**
  524. * vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
  525. * command submissions
  526. *
  527. * @man: The command buffer manager.
  528. * @interruptible: Whether to sleep interruptible when sleeping.
  529. *
  530. * Flushes the current command buffer without allocating a new one. A new one
  531. * is automatically allocated when needed.
  532. */
  533. int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man,
  534. bool interruptible)
  535. {
  536. int ret = vmw_cmdbuf_cur_lock(man, interruptible);
  537. if (ret)
  538. return ret;
  539. __vmw_cmdbuf_cur_flush(man);
  540. vmw_cmdbuf_cur_unlock(man);
  541. return 0;
  542. }
  543. /**
  544. * vmw_cmdbuf_idle - Wait for command buffer manager idle.
  545. *
  546. * @man: The command buffer manager.
  547. * @interruptible: Sleep interruptible while waiting.
  548. * @timeout: Time out after this many ticks.
  549. *
  550. * Wait until the command buffer manager has processed all command buffers,
  551. * or until a timeout occurs. If a timeout occurs, the function will return
  552. * -EBUSY.
  553. */
  554. int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible,
  555. unsigned long timeout)
  556. {
  557. int ret;
  558. ret = vmw_cmdbuf_cur_flush(man, interruptible);
  559. vmw_generic_waiter_add(man->dev_priv,
  560. SVGA_IRQFLAG_COMMAND_BUFFER,
  561. &man->dev_priv->cmdbuf_waiters);
  562. if (interruptible) {
  563. ret = wait_event_interruptible_timeout
  564. (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
  565. timeout);
  566. } else {
  567. ret = wait_event_timeout
  568. (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
  569. timeout);
  570. }
  571. vmw_generic_waiter_remove(man->dev_priv,
  572. SVGA_IRQFLAG_COMMAND_BUFFER,
  573. &man->dev_priv->cmdbuf_waiters);
  574. if (ret == 0) {
  575. if (!vmw_cmdbuf_man_idle(man, true))
  576. ret = -EBUSY;
  577. else
  578. ret = 0;
  579. }
  580. if (ret > 0)
  581. ret = 0;
  582. return ret;
  583. }
  584. /**
  585. * vmw_cmdbuf_try_alloc - Try to allocate buffer space from the main pool.
  586. *
  587. * @man: The command buffer manager.
  588. * @info: Allocation info. Will hold the size on entry and allocated mm node
  589. * on successful return.
  590. *
  591. * Try to allocate buffer space from the main pool. Returns true if succeeded.
  592. * If a fatal error was hit, the error code is returned in @info->ret.
  593. */
  594. static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man *man,
  595. struct vmw_cmdbuf_alloc_info *info)
  596. {
  597. int ret;
  598. if (info->done)
  599. return true;
  600. memset(info->node, 0, sizeof(*info->node));
  601. spin_lock_bh(&man->lock);
  602. ret = drm_mm_insert_node_generic(&man->mm, info->node, info->page_size,
  603. 0, 0,
  604. DRM_MM_SEARCH_DEFAULT,
  605. DRM_MM_CREATE_DEFAULT);
  606. spin_unlock_bh(&man->lock);
  607. info->done = !ret;
  608. return info->done;
  609. }
  610. /**
  611. * vmw_cmdbuf_alloc_space - Allocate buffer space from the main pool.
  612. *
  613. * @man: The command buffer manager.
  614. * @node: Pointer to pre-allocated range-manager node.
  615. * @size: The size of the allocation.
  616. * @interruptible: Whether to sleep interruptible while waiting for space.
  617. *
  618. * This function allocates buffer space from the main pool, and if there is
  619. * no space available ATM, it turns on IRQ handling and sleeps waiting for it to
  620. * become available.
  621. */
  622. static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man *man,
  623. struct drm_mm_node *node,
  624. size_t size,
  625. bool interruptible)
  626. {
  627. struct vmw_cmdbuf_alloc_info info;
  628. info.page_size = PAGE_ALIGN(size) >> PAGE_SHIFT;
  629. info.node = node;
  630. info.done = false;
  631. /*
  632. * To prevent starvation of large requests, only one allocating call
  633. * at a time waiting for space.
  634. */
  635. if (interruptible) {
  636. if (mutex_lock_interruptible(&man->space_mutex))
  637. return -ERESTARTSYS;
  638. } else {
  639. mutex_lock(&man->space_mutex);
  640. }
  641. /* Try to allocate space without waiting. */
  642. if (vmw_cmdbuf_try_alloc(man, &info))
  643. goto out_unlock;
  644. vmw_generic_waiter_add(man->dev_priv,
  645. SVGA_IRQFLAG_COMMAND_BUFFER,
  646. &man->dev_priv->cmdbuf_waiters);
  647. if (interruptible) {
  648. int ret;
  649. ret = wait_event_interruptible
  650. (man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
  651. if (ret) {
  652. vmw_generic_waiter_remove
  653. (man->dev_priv, SVGA_IRQFLAG_COMMAND_BUFFER,
  654. &man->dev_priv->cmdbuf_waiters);
  655. mutex_unlock(&man->space_mutex);
  656. return ret;
  657. }
  658. } else {
  659. wait_event(man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
  660. }
  661. vmw_generic_waiter_remove(man->dev_priv,
  662. SVGA_IRQFLAG_COMMAND_BUFFER,
  663. &man->dev_priv->cmdbuf_waiters);
  664. out_unlock:
  665. mutex_unlock(&man->space_mutex);
  666. return 0;
  667. }
  668. /**
  669. * vmw_cmdbuf_space_pool - Set up a command buffer header with command buffer
  670. * space from the main pool.
  671. *
  672. * @man: The command buffer manager.
  673. * @header: Pointer to the header to set up.
  674. * @size: The requested size of the buffer space.
  675. * @interruptible: Whether to sleep interruptible while waiting for space.
  676. */
  677. static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man *man,
  678. struct vmw_cmdbuf_header *header,
  679. size_t size,
  680. bool interruptible)
  681. {
  682. SVGACBHeader *cb_hdr;
  683. size_t offset;
  684. int ret;
  685. if (!man->has_pool)
  686. return -ENOMEM;
  687. ret = vmw_cmdbuf_alloc_space(man, &header->node, size, interruptible);
  688. if (ret)
  689. return ret;
  690. header->cb_header = dma_pool_alloc(man->headers, GFP_KERNEL,
  691. &header->handle);
  692. if (!header->cb_header) {
  693. ret = -ENOMEM;
  694. goto out_no_cb_header;
  695. }
  696. header->size = header->node.size << PAGE_SHIFT;
  697. cb_hdr = header->cb_header;
  698. offset = header->node.start << PAGE_SHIFT;
  699. header->cmd = man->map + offset;
  700. memset(cb_hdr, 0, sizeof(*cb_hdr));
  701. if (man->using_mob) {
  702. cb_hdr->flags = SVGA_CB_FLAG_MOB;
  703. cb_hdr->ptr.mob.mobid = man->cmd_space->mem.start;
  704. cb_hdr->ptr.mob.mobOffset = offset;
  705. } else {
  706. cb_hdr->ptr.pa = (u64)man->handle + (u64)offset;
  707. }
  708. return 0;
  709. out_no_cb_header:
  710. spin_lock_bh(&man->lock);
  711. drm_mm_remove_node(&header->node);
  712. spin_unlock_bh(&man->lock);
  713. return ret;
  714. }
  715. /**
  716. * vmw_cmdbuf_space_inline - Set up a command buffer header with
  717. * inline command buffer space.
  718. *
  719. * @man: The command buffer manager.
  720. * @header: Pointer to the header to set up.
  721. * @size: The requested size of the buffer space.
  722. */
  723. static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man *man,
  724. struct vmw_cmdbuf_header *header,
  725. int size)
  726. {
  727. struct vmw_cmdbuf_dheader *dheader;
  728. SVGACBHeader *cb_hdr;
  729. if (WARN_ON_ONCE(size > VMW_CMDBUF_INLINE_SIZE))
  730. return -ENOMEM;
  731. dheader = dma_pool_alloc(man->dheaders, GFP_KERNEL,
  732. &header->handle);
  733. if (!dheader)
  734. return -ENOMEM;
  735. header->inline_space = true;
  736. header->size = VMW_CMDBUF_INLINE_SIZE;
  737. cb_hdr = &dheader->cb_header;
  738. header->cb_header = cb_hdr;
  739. header->cmd = dheader->cmd;
  740. memset(dheader, 0, sizeof(*dheader));
  741. cb_hdr->status = SVGA_CB_STATUS_NONE;
  742. cb_hdr->flags = SVGA_CB_FLAG_NONE;
  743. cb_hdr->ptr.pa = (u64)header->handle +
  744. (u64)offsetof(struct vmw_cmdbuf_dheader, cmd);
  745. return 0;
  746. }
  747. /**
  748. * vmw_cmdbuf_alloc - Allocate a command buffer header complete with
  749. * command buffer space.
  750. *
  751. * @man: The command buffer manager.
  752. * @size: The requested size of the buffer space.
  753. * @interruptible: Whether to sleep interruptible while waiting for space.
  754. * @p_header: points to a header pointer to populate on successful return.
  755. *
  756. * Returns a pointer to command buffer space if successful. Otherwise
  757. * returns an error pointer. The header pointer returned in @p_header should
  758. * be used for upcoming calls to vmw_cmdbuf_reserve() and vmw_cmdbuf_commit().
  759. */
  760. void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man,
  761. size_t size, bool interruptible,
  762. struct vmw_cmdbuf_header **p_header)
  763. {
  764. struct vmw_cmdbuf_header *header;
  765. int ret = 0;
  766. *p_header = NULL;
  767. header = kzalloc(sizeof(*header), GFP_KERNEL);
  768. if (!header)
  769. return ERR_PTR(-ENOMEM);
  770. if (size <= VMW_CMDBUF_INLINE_SIZE)
  771. ret = vmw_cmdbuf_space_inline(man, header, size);
  772. else
  773. ret = vmw_cmdbuf_space_pool(man, header, size, interruptible);
  774. if (ret) {
  775. kfree(header);
  776. return ERR_PTR(ret);
  777. }
  778. header->man = man;
  779. INIT_LIST_HEAD(&header->list);
  780. header->cb_header->status = SVGA_CB_STATUS_NONE;
  781. *p_header = header;
  782. return header->cmd;
  783. }
  784. /**
  785. * vmw_cmdbuf_reserve_cur - Reserve space for commands in the current
  786. * command buffer.
  787. *
  788. * @man: The command buffer manager.
  789. * @size: The requested size of the commands.
  790. * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
  791. * @interruptible: Whether to sleep interruptible while waiting for space.
  792. *
  793. * Returns a pointer to command buffer space if successful. Otherwise
  794. * returns an error pointer.
  795. */
  796. static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man *man,
  797. size_t size,
  798. int ctx_id,
  799. bool interruptible)
  800. {
  801. struct vmw_cmdbuf_header *cur;
  802. void *ret;
  803. if (vmw_cmdbuf_cur_lock(man, interruptible))
  804. return ERR_PTR(-ERESTARTSYS);
  805. cur = man->cur;
  806. if (cur && (size + man->cur_pos > cur->size ||
  807. (ctx_id != SVGA3D_INVALID_ID &&
  808. (cur->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT) &&
  809. ctx_id != cur->cb_header->dxContext)))
  810. __vmw_cmdbuf_cur_flush(man);
  811. if (!man->cur) {
  812. ret = vmw_cmdbuf_alloc(man,
  813. max_t(size_t, size, man->default_size),
  814. interruptible, &man->cur);
  815. if (IS_ERR(ret)) {
  816. vmw_cmdbuf_cur_unlock(man);
  817. return ret;
  818. }
  819. cur = man->cur;
  820. }
  821. if (ctx_id != SVGA3D_INVALID_ID) {
  822. cur->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  823. cur->cb_header->dxContext = ctx_id;
  824. }
  825. cur->reserved = size;
  826. return (void *) (man->cur->cmd + man->cur_pos);
  827. }
  828. /**
  829. * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer.
  830. *
  831. * @man: The command buffer manager.
  832. * @size: The size of the commands actually written.
  833. * @flush: Whether to flush the command buffer immediately.
  834. */
  835. static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man *man,
  836. size_t size, bool flush)
  837. {
  838. struct vmw_cmdbuf_header *cur = man->cur;
  839. WARN_ON(!mutex_is_locked(&man->cur_mutex));
  840. WARN_ON(size > cur->reserved);
  841. man->cur_pos += size;
  842. if (!size)
  843. cur->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  844. if (flush)
  845. __vmw_cmdbuf_cur_flush(man);
  846. vmw_cmdbuf_cur_unlock(man);
  847. }
  848. /**
  849. * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer.
  850. *
  851. * @man: The command buffer manager.
  852. * @size: The requested size of the commands.
  853. * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
  854. * @interruptible: Whether to sleep interruptible while waiting for space.
  855. * @header: Header of the command buffer. NULL if the current command buffer
  856. * should be used.
  857. *
  858. * Returns a pointer to command buffer space if successful. Otherwise
  859. * returns an error pointer.
  860. */
  861. void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size,
  862. int ctx_id, bool interruptible,
  863. struct vmw_cmdbuf_header *header)
  864. {
  865. if (!header)
  866. return vmw_cmdbuf_reserve_cur(man, size, ctx_id, interruptible);
  867. if (size > header->size)
  868. return ERR_PTR(-EINVAL);
  869. if (ctx_id != SVGA3D_INVALID_ID) {
  870. header->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  871. header->cb_header->dxContext = ctx_id;
  872. }
  873. header->reserved = size;
  874. return header->cmd;
  875. }
  876. /**
  877. * vmw_cmdbuf_commit - Commit commands in a command buffer.
  878. *
  879. * @man: The command buffer manager.
  880. * @size: The size of the commands actually written.
  881. * @header: Header of the command buffer. NULL if the current command buffer
  882. * should be used.
  883. * @flush: Whether to flush the command buffer immediately.
  884. */
  885. void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size,
  886. struct vmw_cmdbuf_header *header, bool flush)
  887. {
  888. if (!header) {
  889. vmw_cmdbuf_commit_cur(man, size, flush);
  890. return;
  891. }
  892. (void) vmw_cmdbuf_cur_lock(man, false);
  893. __vmw_cmdbuf_cur_flush(man);
  894. WARN_ON(size > header->reserved);
  895. man->cur = header;
  896. man->cur_pos = size;
  897. if (!size)
  898. header->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  899. if (flush)
  900. __vmw_cmdbuf_cur_flush(man);
  901. vmw_cmdbuf_cur_unlock(man);
  902. }
  903. /**
  904. * vmw_cmdbuf_tasklet_schedule - Schedule the interrupt handler bottom half.
  905. *
  906. * @man: The command buffer manager.
  907. */
  908. void vmw_cmdbuf_tasklet_schedule(struct vmw_cmdbuf_man *man)
  909. {
  910. if (!man)
  911. return;
  912. tasklet_schedule(&man->tasklet);
  913. }
  914. /**
  915. * vmw_cmdbuf_send_device_command - Send a command through the device context.
  916. *
  917. * @man: The command buffer manager.
  918. * @command: Pointer to the command to send.
  919. * @size: Size of the command.
  920. *
  921. * Synchronously sends a device context command.
  922. */
  923. static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man *man,
  924. const void *command,
  925. size_t size)
  926. {
  927. struct vmw_cmdbuf_header *header;
  928. int status;
  929. void *cmd = vmw_cmdbuf_alloc(man, size, false, &header);
  930. if (IS_ERR(cmd))
  931. return PTR_ERR(cmd);
  932. memcpy(cmd, command, size);
  933. header->cb_header->length = size;
  934. header->cb_context = SVGA_CB_CONTEXT_DEVICE;
  935. spin_lock_bh(&man->lock);
  936. status = vmw_cmdbuf_header_submit(header);
  937. spin_unlock_bh(&man->lock);
  938. vmw_cmdbuf_header_free(header);
  939. if (status != SVGA_CB_STATUS_COMPLETED) {
  940. DRM_ERROR("Device context command failed with status %d\n",
  941. status);
  942. return -EINVAL;
  943. }
  944. return 0;
  945. }
  946. /**
  947. * vmw_cmdbuf_startstop - Send a start / stop command through the device
  948. * context.
  949. *
  950. * @man: The command buffer manager.
  951. * @enable: Whether to enable or disable the context.
  952. *
  953. * Synchronously sends a device start / stop context command.
  954. */
  955. static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man,
  956. bool enable)
  957. {
  958. struct {
  959. uint32 id;
  960. SVGADCCmdStartStop body;
  961. } __packed cmd;
  962. cmd.id = SVGA_DC_CMD_START_STOP_CONTEXT;
  963. cmd.body.enable = (enable) ? 1 : 0;
  964. cmd.body.context = SVGA_CB_CONTEXT_0;
  965. return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd));
  966. }
  967. /**
  968. * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes
  969. *
  970. * @man: The command buffer manager.
  971. * @size: The size of the main space pool.
  972. * @default_size: The default size of the command buffer for small kernel
  973. * submissions.
  974. *
  975. * Set the size and allocate the main command buffer space pool,
  976. * as well as the default size of the command buffer for
  977. * small kernel submissions. If successful, this enables large command
  978. * submissions. Note that this function requires that rudimentary command
  979. * submission is already available and that the MOB memory manager is alive.
  980. * Returns 0 on success. Negative error code on failure.
  981. */
  982. int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man,
  983. size_t size, size_t default_size)
  984. {
  985. struct vmw_private *dev_priv = man->dev_priv;
  986. bool dummy;
  987. int ret;
  988. if (man->has_pool)
  989. return -EINVAL;
  990. /* First, try to allocate a huge chunk of DMA memory */
  991. size = PAGE_ALIGN(size);
  992. man->map = dma_alloc_coherent(&dev_priv->dev->pdev->dev, size,
  993. &man->handle, GFP_KERNEL);
  994. if (man->map) {
  995. man->using_mob = false;
  996. } else {
  997. /*
  998. * DMA memory failed. If we can have command buffers in a
  999. * MOB, try to use that instead. Note that this will
  1000. * actually call into the already enabled manager, when
  1001. * binding the MOB.
  1002. */
  1003. if (!(dev_priv->capabilities & SVGA_CAP_CMD_BUFFERS_3))
  1004. return -ENOMEM;
  1005. ret = ttm_bo_create(&dev_priv->bdev, size, ttm_bo_type_device,
  1006. &vmw_mob_ne_placement, 0, false, NULL,
  1007. &man->cmd_space);
  1008. if (ret)
  1009. return ret;
  1010. man->using_mob = true;
  1011. ret = ttm_bo_kmap(man->cmd_space, 0, size >> PAGE_SHIFT,
  1012. &man->map_obj);
  1013. if (ret)
  1014. goto out_no_map;
  1015. man->map = ttm_kmap_obj_virtual(&man->map_obj, &dummy);
  1016. }
  1017. man->size = size;
  1018. drm_mm_init(&man->mm, 0, size >> PAGE_SHIFT);
  1019. man->has_pool = true;
  1020. man->default_size = default_size;
  1021. DRM_INFO("Using command buffers with %s pool.\n",
  1022. (man->using_mob) ? "MOB" : "DMA");
  1023. return 0;
  1024. out_no_map:
  1025. if (man->using_mob)
  1026. ttm_bo_unref(&man->cmd_space);
  1027. return ret;
  1028. }
  1029. /**
  1030. * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for
  1031. * inline command buffer submissions only.
  1032. *
  1033. * @dev_priv: Pointer to device private structure.
  1034. *
  1035. * Returns a pointer to a cummand buffer manager to success or error pointer
  1036. * on failure. The command buffer manager will be enabled for submissions of
  1037. * size VMW_CMDBUF_INLINE_SIZE only.
  1038. */
  1039. struct vmw_cmdbuf_man *vmw_cmdbuf_man_create(struct vmw_private *dev_priv)
  1040. {
  1041. struct vmw_cmdbuf_man *man;
  1042. struct vmw_cmdbuf_context *ctx;
  1043. int i;
  1044. int ret;
  1045. if (!(dev_priv->capabilities & SVGA_CAP_COMMAND_BUFFERS))
  1046. return ERR_PTR(-ENOSYS);
  1047. man = kzalloc(sizeof(*man), GFP_KERNEL);
  1048. if (!man)
  1049. return ERR_PTR(-ENOMEM);
  1050. man->headers = dma_pool_create("vmwgfx cmdbuf",
  1051. &dev_priv->dev->pdev->dev,
  1052. sizeof(SVGACBHeader),
  1053. 64, PAGE_SIZE);
  1054. if (!man->headers) {
  1055. ret = -ENOMEM;
  1056. goto out_no_pool;
  1057. }
  1058. man->dheaders = dma_pool_create("vmwgfx inline cmdbuf",
  1059. &dev_priv->dev->pdev->dev,
  1060. sizeof(struct vmw_cmdbuf_dheader),
  1061. 64, PAGE_SIZE);
  1062. if (!man->dheaders) {
  1063. ret = -ENOMEM;
  1064. goto out_no_dpool;
  1065. }
  1066. for_each_cmdbuf_ctx(man, i, ctx)
  1067. vmw_cmdbuf_ctx_init(ctx);
  1068. INIT_LIST_HEAD(&man->error);
  1069. spin_lock_init(&man->lock);
  1070. mutex_init(&man->cur_mutex);
  1071. mutex_init(&man->space_mutex);
  1072. tasklet_init(&man->tasklet, vmw_cmdbuf_man_tasklet,
  1073. (unsigned long) man);
  1074. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1075. init_waitqueue_head(&man->alloc_queue);
  1076. init_waitqueue_head(&man->idle_queue);
  1077. man->dev_priv = dev_priv;
  1078. man->max_hw_submitted = SVGA_CB_MAX_QUEUED_PER_CONTEXT - 1;
  1079. INIT_WORK(&man->work, &vmw_cmdbuf_work_func);
  1080. vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ERROR,
  1081. &dev_priv->error_waiters);
  1082. ret = vmw_cmdbuf_startstop(man, true);
  1083. if (ret) {
  1084. DRM_ERROR("Failed starting command buffer context 0.\n");
  1085. vmw_cmdbuf_man_destroy(man);
  1086. return ERR_PTR(ret);
  1087. }
  1088. return man;
  1089. out_no_dpool:
  1090. dma_pool_destroy(man->headers);
  1091. out_no_pool:
  1092. kfree(man);
  1093. return ERR_PTR(ret);
  1094. }
  1095. /**
  1096. * vmw_cmdbuf_remove_pool - Take down the main buffer space pool.
  1097. *
  1098. * @man: Pointer to a command buffer manager.
  1099. *
  1100. * This function removes the main buffer space pool, and should be called
  1101. * before MOB memory management is removed. When this function has been called,
  1102. * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or
  1103. * less are allowed, and the default size of the command buffer for small kernel
  1104. * submissions is also set to this size.
  1105. */
  1106. void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man)
  1107. {
  1108. if (!man->has_pool)
  1109. return;
  1110. man->has_pool = false;
  1111. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1112. (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1113. if (man->using_mob) {
  1114. (void) ttm_bo_kunmap(&man->map_obj);
  1115. ttm_bo_unref(&man->cmd_space);
  1116. } else {
  1117. dma_free_coherent(&man->dev_priv->dev->pdev->dev,
  1118. man->size, man->map, man->handle);
  1119. }
  1120. }
  1121. /**
  1122. * vmw_cmdbuf_man_destroy - Take down a command buffer manager.
  1123. *
  1124. * @man: Pointer to a command buffer manager.
  1125. *
  1126. * This function idles and then destroys a command buffer manager.
  1127. */
  1128. void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man)
  1129. {
  1130. WARN_ON_ONCE(man->has_pool);
  1131. (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1132. if (vmw_cmdbuf_startstop(man, false))
  1133. DRM_ERROR("Failed stopping command buffer context 0.\n");
  1134. vmw_generic_waiter_remove(man->dev_priv, SVGA_IRQFLAG_ERROR,
  1135. &man->dev_priv->error_waiters);
  1136. tasklet_kill(&man->tasklet);
  1137. (void) cancel_work_sync(&man->work);
  1138. dma_pool_destroy(man->dheaders);
  1139. dma_pool_destroy(man->headers);
  1140. mutex_destroy(&man->cur_mutex);
  1141. mutex_destroy(&man->space_mutex);
  1142. kfree(man);
  1143. }