vmwgfx_cmdbuf.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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;
  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. (ctx_id != SVGA3D_INVALID_ID &&
  811. (cur->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT) &&
  812. ctx_id != cur->cb_header->dxContext)))
  813. __vmw_cmdbuf_cur_flush(man);
  814. if (!man->cur) {
  815. ret = vmw_cmdbuf_alloc(man,
  816. max_t(size_t, size, man->default_size),
  817. interruptible, &man->cur);
  818. if (IS_ERR(ret)) {
  819. vmw_cmdbuf_cur_unlock(man);
  820. return ret;
  821. }
  822. cur = man->cur;
  823. }
  824. if (ctx_id != SVGA3D_INVALID_ID) {
  825. cur->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  826. cur->cb_header->dxContext = ctx_id;
  827. }
  828. cur->reserved = size;
  829. return (void *) (man->cur->cmd + man->cur_pos);
  830. }
  831. /**
  832. * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer.
  833. *
  834. * @man: The command buffer manager.
  835. * @size: The size of the commands actually written.
  836. * @flush: Whether to flush the command buffer immediately.
  837. */
  838. static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man *man,
  839. size_t size, bool flush)
  840. {
  841. struct vmw_cmdbuf_header *cur = man->cur;
  842. WARN_ON(!mutex_is_locked(&man->cur_mutex));
  843. WARN_ON(size > cur->reserved);
  844. man->cur_pos += size;
  845. if (!size)
  846. cur->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  847. if (flush)
  848. __vmw_cmdbuf_cur_flush(man);
  849. vmw_cmdbuf_cur_unlock(man);
  850. }
  851. /**
  852. * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer.
  853. *
  854. * @man: The command buffer manager.
  855. * @size: The requested size of the commands.
  856. * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
  857. * @interruptible: Whether to sleep interruptible while waiting for space.
  858. * @header: Header of the command buffer. NULL if the current command buffer
  859. * should be used.
  860. *
  861. * Returns a pointer to command buffer space if successful. Otherwise
  862. * returns an error pointer.
  863. */
  864. void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size,
  865. int ctx_id, bool interruptible,
  866. struct vmw_cmdbuf_header *header)
  867. {
  868. if (!header)
  869. return vmw_cmdbuf_reserve_cur(man, size, ctx_id, interruptible);
  870. if (size > header->size)
  871. return ERR_PTR(-EINVAL);
  872. if (ctx_id != SVGA3D_INVALID_ID) {
  873. header->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  874. header->cb_header->dxContext = ctx_id;
  875. }
  876. header->reserved = size;
  877. return header->cmd;
  878. }
  879. /**
  880. * vmw_cmdbuf_commit - Commit commands in a command buffer.
  881. *
  882. * @man: The command buffer manager.
  883. * @size: The size of the commands actually written.
  884. * @header: Header of the command buffer. NULL if the current command buffer
  885. * should be used.
  886. * @flush: Whether to flush the command buffer immediately.
  887. */
  888. void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size,
  889. struct vmw_cmdbuf_header *header, bool flush)
  890. {
  891. if (!header) {
  892. vmw_cmdbuf_commit_cur(man, size, flush);
  893. return;
  894. }
  895. (void) vmw_cmdbuf_cur_lock(man, false);
  896. __vmw_cmdbuf_cur_flush(man);
  897. WARN_ON(size > header->reserved);
  898. man->cur = header;
  899. man->cur_pos = size;
  900. if (!size)
  901. header->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  902. if (flush)
  903. __vmw_cmdbuf_cur_flush(man);
  904. vmw_cmdbuf_cur_unlock(man);
  905. }
  906. /**
  907. * vmw_cmdbuf_tasklet_schedule - Schedule the interrupt handler bottom half.
  908. *
  909. * @man: The command buffer manager.
  910. */
  911. void vmw_cmdbuf_tasklet_schedule(struct vmw_cmdbuf_man *man)
  912. {
  913. if (!man)
  914. return;
  915. tasklet_schedule(&man->tasklet);
  916. }
  917. /**
  918. * vmw_cmdbuf_send_device_command - Send a command through the device context.
  919. *
  920. * @man: The command buffer manager.
  921. * @command: Pointer to the command to send.
  922. * @size: Size of the command.
  923. *
  924. * Synchronously sends a device context command.
  925. */
  926. static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man *man,
  927. const void *command,
  928. size_t size)
  929. {
  930. struct vmw_cmdbuf_header *header;
  931. int status;
  932. void *cmd = vmw_cmdbuf_alloc(man, size, false, &header);
  933. if (IS_ERR(cmd))
  934. return PTR_ERR(cmd);
  935. memcpy(cmd, command, size);
  936. header->cb_header->length = size;
  937. header->cb_context = SVGA_CB_CONTEXT_DEVICE;
  938. spin_lock_bh(&man->lock);
  939. status = vmw_cmdbuf_header_submit(header);
  940. spin_unlock_bh(&man->lock);
  941. vmw_cmdbuf_header_free(header);
  942. if (status != SVGA_CB_STATUS_COMPLETED) {
  943. DRM_ERROR("Device context command failed with status %d\n",
  944. status);
  945. return -EINVAL;
  946. }
  947. return 0;
  948. }
  949. /**
  950. * vmw_cmdbuf_startstop - Send a start / stop command through the device
  951. * context.
  952. *
  953. * @man: The command buffer manager.
  954. * @enable: Whether to enable or disable the context.
  955. *
  956. * Synchronously sends a device start / stop context command.
  957. */
  958. static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man,
  959. bool enable)
  960. {
  961. struct {
  962. uint32 id;
  963. SVGADCCmdStartStop body;
  964. } __packed cmd;
  965. cmd.id = SVGA_DC_CMD_START_STOP_CONTEXT;
  966. cmd.body.enable = (enable) ? 1 : 0;
  967. cmd.body.context = SVGA_CB_CONTEXT_0;
  968. return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd));
  969. }
  970. /**
  971. * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes
  972. *
  973. * @man: The command buffer manager.
  974. * @size: The size of the main space pool.
  975. * @default_size: The default size of the command buffer for small kernel
  976. * submissions.
  977. *
  978. * Set the size and allocate the main command buffer space pool,
  979. * as well as the default size of the command buffer for
  980. * small kernel submissions. If successful, this enables large command
  981. * submissions. Note that this function requires that rudimentary command
  982. * submission is already available and that the MOB memory manager is alive.
  983. * Returns 0 on success. Negative error code on failure.
  984. */
  985. int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man,
  986. size_t size, size_t default_size)
  987. {
  988. struct vmw_private *dev_priv = man->dev_priv;
  989. bool dummy;
  990. int ret;
  991. if (man->has_pool)
  992. return -EINVAL;
  993. /* First, try to allocate a huge chunk of DMA memory */
  994. size = PAGE_ALIGN(size);
  995. man->map = dma_alloc_coherent(&dev_priv->dev->pdev->dev, size,
  996. &man->handle, GFP_KERNEL);
  997. if (man->map) {
  998. man->using_mob = false;
  999. } else {
  1000. /*
  1001. * DMA memory failed. If we can have command buffers in a
  1002. * MOB, try to use that instead. Note that this will
  1003. * actually call into the already enabled manager, when
  1004. * binding the MOB.
  1005. */
  1006. if (!(dev_priv->capabilities & SVGA_CAP_CMD_BUFFERS_3))
  1007. return -ENOMEM;
  1008. ret = ttm_bo_create(&dev_priv->bdev, size, ttm_bo_type_device,
  1009. &vmw_mob_ne_placement, 0, false, NULL,
  1010. &man->cmd_space);
  1011. if (ret)
  1012. return ret;
  1013. man->using_mob = true;
  1014. ret = ttm_bo_kmap(man->cmd_space, 0, size >> PAGE_SHIFT,
  1015. &man->map_obj);
  1016. if (ret)
  1017. goto out_no_map;
  1018. man->map = ttm_kmap_obj_virtual(&man->map_obj, &dummy);
  1019. }
  1020. man->size = size;
  1021. drm_mm_init(&man->mm, 0, size >> PAGE_SHIFT);
  1022. man->has_pool = true;
  1023. man->default_size = default_size;
  1024. DRM_INFO("Using command buffers with %s pool.\n",
  1025. (man->using_mob) ? "MOB" : "DMA");
  1026. return 0;
  1027. out_no_map:
  1028. if (man->using_mob)
  1029. ttm_bo_unref(&man->cmd_space);
  1030. return ret;
  1031. }
  1032. /**
  1033. * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for
  1034. * inline command buffer submissions only.
  1035. *
  1036. * @dev_priv: Pointer to device private structure.
  1037. *
  1038. * Returns a pointer to a cummand buffer manager to success or error pointer
  1039. * on failure. The command buffer manager will be enabled for submissions of
  1040. * size VMW_CMDBUF_INLINE_SIZE only.
  1041. */
  1042. struct vmw_cmdbuf_man *vmw_cmdbuf_man_create(struct vmw_private *dev_priv)
  1043. {
  1044. struct vmw_cmdbuf_man *man;
  1045. struct vmw_cmdbuf_context *ctx;
  1046. int i;
  1047. int ret;
  1048. if (!(dev_priv->capabilities & SVGA_CAP_COMMAND_BUFFERS))
  1049. return ERR_PTR(-ENOSYS);
  1050. man = kzalloc(sizeof(*man), GFP_KERNEL);
  1051. if (!man)
  1052. return ERR_PTR(-ENOMEM);
  1053. man->headers = dma_pool_create("vmwgfx cmdbuf",
  1054. &dev_priv->dev->pdev->dev,
  1055. sizeof(SVGACBHeader),
  1056. 64, PAGE_SIZE);
  1057. if (!man->headers) {
  1058. ret = -ENOMEM;
  1059. goto out_no_pool;
  1060. }
  1061. man->dheaders = dma_pool_create("vmwgfx inline cmdbuf",
  1062. &dev_priv->dev->pdev->dev,
  1063. sizeof(struct vmw_cmdbuf_dheader),
  1064. 64, PAGE_SIZE);
  1065. if (!man->dheaders) {
  1066. ret = -ENOMEM;
  1067. goto out_no_dpool;
  1068. }
  1069. for_each_cmdbuf_ctx(man, i, ctx)
  1070. vmw_cmdbuf_ctx_init(ctx);
  1071. INIT_LIST_HEAD(&man->error);
  1072. spin_lock_init(&man->lock);
  1073. mutex_init(&man->cur_mutex);
  1074. mutex_init(&man->space_mutex);
  1075. tasklet_init(&man->tasklet, vmw_cmdbuf_man_tasklet,
  1076. (unsigned long) man);
  1077. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1078. init_waitqueue_head(&man->alloc_queue);
  1079. init_waitqueue_head(&man->idle_queue);
  1080. man->dev_priv = dev_priv;
  1081. man->max_hw_submitted = SVGA_CB_MAX_QUEUED_PER_CONTEXT - 1;
  1082. INIT_WORK(&man->work, &vmw_cmdbuf_work_func);
  1083. vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ERROR,
  1084. &dev_priv->error_waiters);
  1085. ret = vmw_cmdbuf_startstop(man, true);
  1086. if (ret) {
  1087. DRM_ERROR("Failed starting command buffer context 0.\n");
  1088. vmw_cmdbuf_man_destroy(man);
  1089. return ERR_PTR(ret);
  1090. }
  1091. return man;
  1092. out_no_dpool:
  1093. dma_pool_destroy(man->headers);
  1094. out_no_pool:
  1095. kfree(man);
  1096. return ERR_PTR(ret);
  1097. }
  1098. /**
  1099. * vmw_cmdbuf_remove_pool - Take down the main buffer space pool.
  1100. *
  1101. * @man: Pointer to a command buffer manager.
  1102. *
  1103. * This function removes the main buffer space pool, and should be called
  1104. * before MOB memory management is removed. When this function has been called,
  1105. * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or
  1106. * less are allowed, and the default size of the command buffer for small kernel
  1107. * submissions is also set to this size.
  1108. */
  1109. void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man)
  1110. {
  1111. if (!man->has_pool)
  1112. return;
  1113. man->has_pool = false;
  1114. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1115. (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1116. if (man->using_mob) {
  1117. (void) ttm_bo_kunmap(&man->map_obj);
  1118. ttm_bo_unref(&man->cmd_space);
  1119. } else {
  1120. dma_free_coherent(&man->dev_priv->dev->pdev->dev,
  1121. man->size, man->map, man->handle);
  1122. }
  1123. }
  1124. /**
  1125. * vmw_cmdbuf_man_destroy - Take down a command buffer manager.
  1126. *
  1127. * @man: Pointer to a command buffer manager.
  1128. *
  1129. * This function idles and then destroys a command buffer manager.
  1130. */
  1131. void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man)
  1132. {
  1133. WARN_ON_ONCE(man->has_pool);
  1134. (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1135. if (vmw_cmdbuf_startstop(man, false))
  1136. DRM_ERROR("Failed stopping command buffer context 0.\n");
  1137. vmw_generic_waiter_remove(man->dev_priv, SVGA_IRQFLAG_ERROR,
  1138. &man->dev_priv->error_waiters);
  1139. tasklet_kill(&man->tasklet);
  1140. (void) cancel_work_sync(&man->work);
  1141. dma_pool_destroy(man->dheaders);
  1142. dma_pool_destroy(man->headers);
  1143. mutex_destroy(&man->cur_mutex);
  1144. mutex_destroy(&man->space_mutex);
  1145. kfree(man);
  1146. }