vmwgfx_cmdbuf.c 39 KB

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