vmwgfx_shader.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2009-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_placement.h>
  28. #include "vmwgfx_drv.h"
  29. #include "vmwgfx_resource_priv.h"
  30. #include "vmwgfx_binding.h"
  31. struct vmw_shader {
  32. struct vmw_resource res;
  33. SVGA3dShaderType type;
  34. uint32_t size;
  35. uint8_t num_input_sig;
  36. uint8_t num_output_sig;
  37. };
  38. struct vmw_user_shader {
  39. struct ttm_base_object base;
  40. struct vmw_shader shader;
  41. };
  42. struct vmw_dx_shader {
  43. struct vmw_resource res;
  44. struct vmw_resource *ctx;
  45. struct vmw_resource *cotable;
  46. u32 id;
  47. bool committed;
  48. struct list_head cotable_head;
  49. };
  50. static void vmw_user_shader_free(struct vmw_resource *res);
  51. static struct vmw_resource *
  52. vmw_user_shader_base_to_res(struct ttm_base_object *base);
  53. static int vmw_gb_shader_create(struct vmw_resource *res);
  54. static int vmw_gb_shader_bind(struct vmw_resource *res,
  55. struct ttm_validate_buffer *val_buf);
  56. static int vmw_gb_shader_unbind(struct vmw_resource *res,
  57. bool readback,
  58. struct ttm_validate_buffer *val_buf);
  59. static int vmw_gb_shader_destroy(struct vmw_resource *res);
  60. static int vmw_dx_shader_create(struct vmw_resource *res);
  61. static int vmw_dx_shader_bind(struct vmw_resource *res,
  62. struct ttm_validate_buffer *val_buf);
  63. static int vmw_dx_shader_unbind(struct vmw_resource *res,
  64. bool readback,
  65. struct ttm_validate_buffer *val_buf);
  66. static void vmw_dx_shader_commit_notify(struct vmw_resource *res,
  67. enum vmw_cmdbuf_res_state state);
  68. static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type);
  69. static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type);
  70. static const struct vmw_user_resource_conv user_shader_conv = {
  71. .object_type = VMW_RES_SHADER,
  72. .base_obj_to_res = vmw_user_shader_base_to_res,
  73. .res_free = vmw_user_shader_free
  74. };
  75. const struct vmw_user_resource_conv *user_shader_converter =
  76. &user_shader_conv;
  77. static const struct vmw_res_func vmw_gb_shader_func = {
  78. .res_type = vmw_res_shader,
  79. .needs_backup = true,
  80. .may_evict = true,
  81. .prio = 3,
  82. .dirty_prio = 3,
  83. .type_name = "guest backed shaders",
  84. .backup_placement = &vmw_mob_placement,
  85. .create = vmw_gb_shader_create,
  86. .destroy = vmw_gb_shader_destroy,
  87. .bind = vmw_gb_shader_bind,
  88. .unbind = vmw_gb_shader_unbind
  89. };
  90. static const struct vmw_res_func vmw_dx_shader_func = {
  91. .res_type = vmw_res_shader,
  92. .needs_backup = true,
  93. .may_evict = true,
  94. .prio = 3,
  95. .dirty_prio = 3,
  96. .type_name = "dx shaders",
  97. .backup_placement = &vmw_mob_placement,
  98. .create = vmw_dx_shader_create,
  99. /*
  100. * The destroy callback is only called with a committed resource on
  101. * context destroy, in which case we destroy the cotable anyway,
  102. * so there's no need to destroy DX shaders separately.
  103. */
  104. .destroy = NULL,
  105. .bind = vmw_dx_shader_bind,
  106. .unbind = vmw_dx_shader_unbind,
  107. .commit_notify = vmw_dx_shader_commit_notify,
  108. };
  109. /*
  110. * Shader management:
  111. */
  112. static inline struct vmw_shader *
  113. vmw_res_to_shader(struct vmw_resource *res)
  114. {
  115. return container_of(res, struct vmw_shader, res);
  116. }
  117. /**
  118. * vmw_res_to_dx_shader - typecast a struct vmw_resource to a
  119. * struct vmw_dx_shader
  120. *
  121. * @res: Pointer to the struct vmw_resource.
  122. */
  123. static inline struct vmw_dx_shader *
  124. vmw_res_to_dx_shader(struct vmw_resource *res)
  125. {
  126. return container_of(res, struct vmw_dx_shader, res);
  127. }
  128. static void vmw_hw_shader_destroy(struct vmw_resource *res)
  129. {
  130. if (likely(res->func->destroy))
  131. (void) res->func->destroy(res);
  132. else
  133. res->id = -1;
  134. }
  135. static int vmw_gb_shader_init(struct vmw_private *dev_priv,
  136. struct vmw_resource *res,
  137. uint32_t size,
  138. uint64_t offset,
  139. SVGA3dShaderType type,
  140. uint8_t num_input_sig,
  141. uint8_t num_output_sig,
  142. struct vmw_buffer_object *byte_code,
  143. void (*res_free) (struct vmw_resource *res))
  144. {
  145. struct vmw_shader *shader = vmw_res_to_shader(res);
  146. int ret;
  147. ret = vmw_resource_init(dev_priv, res, true, res_free,
  148. &vmw_gb_shader_func);
  149. if (unlikely(ret != 0)) {
  150. if (res_free)
  151. res_free(res);
  152. else
  153. kfree(res);
  154. return ret;
  155. }
  156. res->backup_size = size;
  157. if (byte_code) {
  158. res->backup = vmw_bo_reference(byte_code);
  159. res->backup_offset = offset;
  160. }
  161. shader->size = size;
  162. shader->type = type;
  163. shader->num_input_sig = num_input_sig;
  164. shader->num_output_sig = num_output_sig;
  165. res->hw_destroy = vmw_hw_shader_destroy;
  166. return 0;
  167. }
  168. /*
  169. * GB shader code:
  170. */
  171. static int vmw_gb_shader_create(struct vmw_resource *res)
  172. {
  173. struct vmw_private *dev_priv = res->dev_priv;
  174. struct vmw_shader *shader = vmw_res_to_shader(res);
  175. int ret;
  176. struct {
  177. SVGA3dCmdHeader header;
  178. SVGA3dCmdDefineGBShader body;
  179. } *cmd;
  180. if (likely(res->id != -1))
  181. return 0;
  182. ret = vmw_resource_alloc_id(res);
  183. if (unlikely(ret != 0)) {
  184. DRM_ERROR("Failed to allocate a shader id.\n");
  185. goto out_no_id;
  186. }
  187. if (unlikely(res->id >= VMWGFX_NUM_GB_SHADER)) {
  188. ret = -EBUSY;
  189. goto out_no_fifo;
  190. }
  191. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  192. if (unlikely(cmd == NULL)) {
  193. ret = -ENOMEM;
  194. goto out_no_fifo;
  195. }
  196. cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SHADER;
  197. cmd->header.size = sizeof(cmd->body);
  198. cmd->body.shid = res->id;
  199. cmd->body.type = shader->type;
  200. cmd->body.sizeInBytes = shader->size;
  201. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  202. vmw_fifo_resource_inc(dev_priv);
  203. return 0;
  204. out_no_fifo:
  205. vmw_resource_release_id(res);
  206. out_no_id:
  207. return ret;
  208. }
  209. static int vmw_gb_shader_bind(struct vmw_resource *res,
  210. struct ttm_validate_buffer *val_buf)
  211. {
  212. struct vmw_private *dev_priv = res->dev_priv;
  213. struct {
  214. SVGA3dCmdHeader header;
  215. SVGA3dCmdBindGBShader body;
  216. } *cmd;
  217. struct ttm_buffer_object *bo = val_buf->bo;
  218. BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
  219. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  220. if (unlikely(cmd == NULL))
  221. return -ENOMEM;
  222. cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER;
  223. cmd->header.size = sizeof(cmd->body);
  224. cmd->body.shid = res->id;
  225. cmd->body.mobid = bo->resource->start;
  226. cmd->body.offsetInBytes = res->backup_offset;
  227. res->backup_dirty = false;
  228. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  229. return 0;
  230. }
  231. static int vmw_gb_shader_unbind(struct vmw_resource *res,
  232. bool readback,
  233. struct ttm_validate_buffer *val_buf)
  234. {
  235. struct vmw_private *dev_priv = res->dev_priv;
  236. struct {
  237. SVGA3dCmdHeader header;
  238. SVGA3dCmdBindGBShader body;
  239. } *cmd;
  240. struct vmw_fence_obj *fence;
  241. BUG_ON(res->backup->base.resource->mem_type != VMW_PL_MOB);
  242. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  243. if (unlikely(cmd == NULL))
  244. return -ENOMEM;
  245. cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER;
  246. cmd->header.size = sizeof(cmd->body);
  247. cmd->body.shid = res->id;
  248. cmd->body.mobid = SVGA3D_INVALID_ID;
  249. cmd->body.offsetInBytes = 0;
  250. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  251. /*
  252. * Create a fence object and fence the backup buffer.
  253. */
  254. (void) vmw_execbuf_fence_commands(NULL, dev_priv,
  255. &fence, NULL);
  256. vmw_bo_fence_single(val_buf->bo, fence);
  257. if (likely(fence != NULL))
  258. vmw_fence_obj_unreference(&fence);
  259. return 0;
  260. }
  261. static int vmw_gb_shader_destroy(struct vmw_resource *res)
  262. {
  263. struct vmw_private *dev_priv = res->dev_priv;
  264. struct {
  265. SVGA3dCmdHeader header;
  266. SVGA3dCmdDestroyGBShader body;
  267. } *cmd;
  268. if (likely(res->id == -1))
  269. return 0;
  270. mutex_lock(&dev_priv->binding_mutex);
  271. vmw_binding_res_list_scrub(&res->binding_head);
  272. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  273. if (unlikely(cmd == NULL)) {
  274. mutex_unlock(&dev_priv->binding_mutex);
  275. return -ENOMEM;
  276. }
  277. cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SHADER;
  278. cmd->header.size = sizeof(cmd->body);
  279. cmd->body.shid = res->id;
  280. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  281. mutex_unlock(&dev_priv->binding_mutex);
  282. vmw_resource_release_id(res);
  283. vmw_fifo_resource_dec(dev_priv);
  284. return 0;
  285. }
  286. /*
  287. * DX shader code:
  288. */
  289. /**
  290. * vmw_dx_shader_commit_notify - Notify that a shader operation has been
  291. * committed to hardware from a user-supplied command stream.
  292. *
  293. * @res: Pointer to the shader resource.
  294. * @state: Indicating whether a creation or removal has been committed.
  295. *
  296. */
  297. static void vmw_dx_shader_commit_notify(struct vmw_resource *res,
  298. enum vmw_cmdbuf_res_state state)
  299. {
  300. struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
  301. struct vmw_private *dev_priv = res->dev_priv;
  302. if (state == VMW_CMDBUF_RES_ADD) {
  303. mutex_lock(&dev_priv->binding_mutex);
  304. vmw_cotable_add_resource(shader->cotable,
  305. &shader->cotable_head);
  306. shader->committed = true;
  307. res->id = shader->id;
  308. mutex_unlock(&dev_priv->binding_mutex);
  309. } else {
  310. mutex_lock(&dev_priv->binding_mutex);
  311. list_del_init(&shader->cotable_head);
  312. shader->committed = false;
  313. res->id = -1;
  314. mutex_unlock(&dev_priv->binding_mutex);
  315. }
  316. }
  317. /**
  318. * vmw_dx_shader_unscrub - Have the device reattach a MOB to a DX shader.
  319. *
  320. * @res: The shader resource
  321. *
  322. * This function reverts a scrub operation.
  323. */
  324. static int vmw_dx_shader_unscrub(struct vmw_resource *res)
  325. {
  326. struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
  327. struct vmw_private *dev_priv = res->dev_priv;
  328. struct {
  329. SVGA3dCmdHeader header;
  330. SVGA3dCmdDXBindShader body;
  331. } *cmd;
  332. if (!list_empty(&shader->cotable_head) || !shader->committed)
  333. return 0;
  334. cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), shader->ctx->id);
  335. if (unlikely(cmd == NULL))
  336. return -ENOMEM;
  337. cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER;
  338. cmd->header.size = sizeof(cmd->body);
  339. cmd->body.cid = shader->ctx->id;
  340. cmd->body.shid = shader->id;
  341. cmd->body.mobid = res->backup->base.resource->start;
  342. cmd->body.offsetInBytes = res->backup_offset;
  343. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  344. vmw_cotable_add_resource(shader->cotable, &shader->cotable_head);
  345. return 0;
  346. }
  347. /**
  348. * vmw_dx_shader_create - The DX shader create callback
  349. *
  350. * @res: The DX shader resource
  351. *
  352. * The create callback is called as part of resource validation and
  353. * makes sure that we unscrub the shader if it's previously been scrubbed.
  354. */
  355. static int vmw_dx_shader_create(struct vmw_resource *res)
  356. {
  357. struct vmw_private *dev_priv = res->dev_priv;
  358. struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
  359. int ret = 0;
  360. WARN_ON_ONCE(!shader->committed);
  361. if (vmw_resource_mob_attached(res)) {
  362. mutex_lock(&dev_priv->binding_mutex);
  363. ret = vmw_dx_shader_unscrub(res);
  364. mutex_unlock(&dev_priv->binding_mutex);
  365. }
  366. res->id = shader->id;
  367. return ret;
  368. }
  369. /**
  370. * vmw_dx_shader_bind - The DX shader bind callback
  371. *
  372. * @res: The DX shader resource
  373. * @val_buf: Pointer to the validate buffer.
  374. *
  375. */
  376. static int vmw_dx_shader_bind(struct vmw_resource *res,
  377. struct ttm_validate_buffer *val_buf)
  378. {
  379. struct vmw_private *dev_priv = res->dev_priv;
  380. struct ttm_buffer_object *bo = val_buf->bo;
  381. BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
  382. mutex_lock(&dev_priv->binding_mutex);
  383. vmw_dx_shader_unscrub(res);
  384. mutex_unlock(&dev_priv->binding_mutex);
  385. return 0;
  386. }
  387. /**
  388. * vmw_dx_shader_scrub - Have the device unbind a MOB from a DX shader.
  389. *
  390. * @res: The shader resource
  391. *
  392. * This function unbinds a MOB from the DX shader without requiring the
  393. * MOB dma_buffer to be reserved. The driver still considers the MOB bound.
  394. * However, once the driver eventually decides to unbind the MOB, it doesn't
  395. * need to access the context.
  396. */
  397. static int vmw_dx_shader_scrub(struct vmw_resource *res)
  398. {
  399. struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
  400. struct vmw_private *dev_priv = res->dev_priv;
  401. struct {
  402. SVGA3dCmdHeader header;
  403. SVGA3dCmdDXBindShader body;
  404. } *cmd;
  405. if (list_empty(&shader->cotable_head))
  406. return 0;
  407. WARN_ON_ONCE(!shader->committed);
  408. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  409. if (unlikely(cmd == NULL))
  410. return -ENOMEM;
  411. cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER;
  412. cmd->header.size = sizeof(cmd->body);
  413. cmd->body.cid = shader->ctx->id;
  414. cmd->body.shid = res->id;
  415. cmd->body.mobid = SVGA3D_INVALID_ID;
  416. cmd->body.offsetInBytes = 0;
  417. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  418. res->id = -1;
  419. list_del_init(&shader->cotable_head);
  420. return 0;
  421. }
  422. /**
  423. * vmw_dx_shader_unbind - The dx shader unbind callback.
  424. *
  425. * @res: The shader resource
  426. * @readback: Whether this is a readback unbind. Currently unused.
  427. * @val_buf: MOB buffer information.
  428. */
  429. static int vmw_dx_shader_unbind(struct vmw_resource *res,
  430. bool readback,
  431. struct ttm_validate_buffer *val_buf)
  432. {
  433. struct vmw_private *dev_priv = res->dev_priv;
  434. struct vmw_fence_obj *fence;
  435. int ret;
  436. BUG_ON(res->backup->base.resource->mem_type != VMW_PL_MOB);
  437. mutex_lock(&dev_priv->binding_mutex);
  438. ret = vmw_dx_shader_scrub(res);
  439. mutex_unlock(&dev_priv->binding_mutex);
  440. if (ret)
  441. return ret;
  442. (void) vmw_execbuf_fence_commands(NULL, dev_priv,
  443. &fence, NULL);
  444. vmw_bo_fence_single(val_buf->bo, fence);
  445. if (likely(fence != NULL))
  446. vmw_fence_obj_unreference(&fence);
  447. return 0;
  448. }
  449. /**
  450. * vmw_dx_shader_cotable_list_scrub - The cotable unbind_func callback for
  451. * DX shaders.
  452. *
  453. * @dev_priv: Pointer to device private structure.
  454. * @list: The list of cotable resources.
  455. * @readback: Whether the call was part of a readback unbind.
  456. *
  457. * Scrubs all shader MOBs so that any subsequent shader unbind or shader
  458. * destroy operation won't need to swap in the context.
  459. */
  460. void vmw_dx_shader_cotable_list_scrub(struct vmw_private *dev_priv,
  461. struct list_head *list,
  462. bool readback)
  463. {
  464. struct vmw_dx_shader *entry, *next;
  465. lockdep_assert_held_once(&dev_priv->binding_mutex);
  466. list_for_each_entry_safe(entry, next, list, cotable_head) {
  467. WARN_ON(vmw_dx_shader_scrub(&entry->res));
  468. if (!readback)
  469. entry->committed = false;
  470. }
  471. }
  472. /**
  473. * vmw_dx_shader_res_free - The DX shader free callback
  474. *
  475. * @res: The shader resource
  476. *
  477. * Frees the DX shader resource.
  478. */
  479. static void vmw_dx_shader_res_free(struct vmw_resource *res)
  480. {
  481. struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
  482. vmw_resource_unreference(&shader->cotable);
  483. kfree(shader);
  484. }
  485. /**
  486. * vmw_dx_shader_add - Add a shader resource as a command buffer managed
  487. * resource.
  488. *
  489. * @man: The command buffer resource manager.
  490. * @ctx: Pointer to the context resource.
  491. * @user_key: The id used for this shader.
  492. * @shader_type: The shader type.
  493. * @list: The list of staged command buffer managed resources.
  494. */
  495. int vmw_dx_shader_add(struct vmw_cmdbuf_res_manager *man,
  496. struct vmw_resource *ctx,
  497. u32 user_key,
  498. SVGA3dShaderType shader_type,
  499. struct list_head *list)
  500. {
  501. struct vmw_dx_shader *shader;
  502. struct vmw_resource *res;
  503. struct vmw_private *dev_priv = ctx->dev_priv;
  504. int ret;
  505. if (!vmw_shader_id_ok(user_key, shader_type))
  506. return -EINVAL;
  507. shader = kmalloc(sizeof(*shader), GFP_KERNEL);
  508. if (!shader) {
  509. return -ENOMEM;
  510. }
  511. res = &shader->res;
  512. shader->ctx = ctx;
  513. shader->cotable = vmw_resource_reference
  514. (vmw_context_cotable(ctx, SVGA_COTABLE_DXSHADER));
  515. shader->id = user_key;
  516. shader->committed = false;
  517. INIT_LIST_HEAD(&shader->cotable_head);
  518. ret = vmw_resource_init(dev_priv, res, true,
  519. vmw_dx_shader_res_free, &vmw_dx_shader_func);
  520. if (ret)
  521. goto out_resource_init;
  522. /*
  523. * The user_key name-space is not per shader type for DX shaders,
  524. * so when hashing, use a single zero shader type.
  525. */
  526. ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader,
  527. vmw_shader_key(user_key, 0),
  528. res, list);
  529. if (ret)
  530. goto out_resource_init;
  531. res->id = shader->id;
  532. res->hw_destroy = vmw_hw_shader_destroy;
  533. out_resource_init:
  534. vmw_resource_unreference(&res);
  535. return ret;
  536. }
  537. /*
  538. * User-space shader management:
  539. */
  540. static struct vmw_resource *
  541. vmw_user_shader_base_to_res(struct ttm_base_object *base)
  542. {
  543. return &(container_of(base, struct vmw_user_shader, base)->
  544. shader.res);
  545. }
  546. static void vmw_user_shader_free(struct vmw_resource *res)
  547. {
  548. struct vmw_user_shader *ushader =
  549. container_of(res, struct vmw_user_shader, shader.res);
  550. ttm_base_object_kfree(ushader, base);
  551. }
  552. static void vmw_shader_free(struct vmw_resource *res)
  553. {
  554. struct vmw_shader *shader = vmw_res_to_shader(res);
  555. kfree(shader);
  556. }
  557. /*
  558. * This function is called when user space has no more references on the
  559. * base object. It releases the base-object's reference on the resource object.
  560. */
  561. static void vmw_user_shader_base_release(struct ttm_base_object **p_base)
  562. {
  563. struct ttm_base_object *base = *p_base;
  564. struct vmw_resource *res = vmw_user_shader_base_to_res(base);
  565. *p_base = NULL;
  566. vmw_resource_unreference(&res);
  567. }
  568. int vmw_shader_destroy_ioctl(struct drm_device *dev, void *data,
  569. struct drm_file *file_priv)
  570. {
  571. struct drm_vmw_shader_arg *arg = (struct drm_vmw_shader_arg *)data;
  572. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  573. return ttm_ref_object_base_unref(tfile, arg->handle);
  574. }
  575. static int vmw_user_shader_alloc(struct vmw_private *dev_priv,
  576. struct vmw_buffer_object *buffer,
  577. size_t shader_size,
  578. size_t offset,
  579. SVGA3dShaderType shader_type,
  580. uint8_t num_input_sig,
  581. uint8_t num_output_sig,
  582. struct ttm_object_file *tfile,
  583. u32 *handle)
  584. {
  585. struct vmw_user_shader *ushader;
  586. struct vmw_resource *res, *tmp;
  587. int ret;
  588. ushader = kzalloc(sizeof(*ushader), GFP_KERNEL);
  589. if (unlikely(!ushader)) {
  590. ret = -ENOMEM;
  591. goto out;
  592. }
  593. res = &ushader->shader.res;
  594. ushader->base.shareable = false;
  595. ushader->base.tfile = NULL;
  596. /*
  597. * From here on, the destructor takes over resource freeing.
  598. */
  599. ret = vmw_gb_shader_init(dev_priv, res, shader_size,
  600. offset, shader_type, num_input_sig,
  601. num_output_sig, buffer,
  602. vmw_user_shader_free);
  603. if (unlikely(ret != 0))
  604. goto out;
  605. tmp = vmw_resource_reference(res);
  606. ret = ttm_base_object_init(tfile, &ushader->base, false,
  607. VMW_RES_SHADER,
  608. &vmw_user_shader_base_release);
  609. if (unlikely(ret != 0)) {
  610. vmw_resource_unreference(&tmp);
  611. goto out_err;
  612. }
  613. if (handle)
  614. *handle = ushader->base.handle;
  615. out_err:
  616. vmw_resource_unreference(&res);
  617. out:
  618. return ret;
  619. }
  620. static struct vmw_resource *vmw_shader_alloc(struct vmw_private *dev_priv,
  621. struct vmw_buffer_object *buffer,
  622. size_t shader_size,
  623. size_t offset,
  624. SVGA3dShaderType shader_type)
  625. {
  626. struct vmw_shader *shader;
  627. struct vmw_resource *res;
  628. int ret;
  629. shader = kzalloc(sizeof(*shader), GFP_KERNEL);
  630. if (unlikely(!shader)) {
  631. ret = -ENOMEM;
  632. goto out_err;
  633. }
  634. res = &shader->res;
  635. /*
  636. * From here on, the destructor takes over resource freeing.
  637. */
  638. ret = vmw_gb_shader_init(dev_priv, res, shader_size,
  639. offset, shader_type, 0, 0, buffer,
  640. vmw_shader_free);
  641. out_err:
  642. return ret ? ERR_PTR(ret) : res;
  643. }
  644. static int vmw_shader_define(struct drm_device *dev, struct drm_file *file_priv,
  645. enum drm_vmw_shader_type shader_type_drm,
  646. u32 buffer_handle, size_t size, size_t offset,
  647. uint8_t num_input_sig, uint8_t num_output_sig,
  648. uint32_t *shader_handle)
  649. {
  650. struct vmw_private *dev_priv = vmw_priv(dev);
  651. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  652. struct vmw_buffer_object *buffer = NULL;
  653. SVGA3dShaderType shader_type;
  654. int ret;
  655. if (buffer_handle != SVGA3D_INVALID_ID) {
  656. ret = vmw_user_bo_lookup(file_priv, buffer_handle, &buffer);
  657. if (unlikely(ret != 0)) {
  658. VMW_DEBUG_USER("Couldn't find buffer for shader creation.\n");
  659. return ret;
  660. }
  661. if ((u64)buffer->base.base.size < (u64)size + (u64)offset) {
  662. VMW_DEBUG_USER("Illegal buffer- or shader size.\n");
  663. ret = -EINVAL;
  664. goto out_bad_arg;
  665. }
  666. }
  667. switch (shader_type_drm) {
  668. case drm_vmw_shader_type_vs:
  669. shader_type = SVGA3D_SHADERTYPE_VS;
  670. break;
  671. case drm_vmw_shader_type_ps:
  672. shader_type = SVGA3D_SHADERTYPE_PS;
  673. break;
  674. default:
  675. VMW_DEBUG_USER("Illegal shader type.\n");
  676. ret = -EINVAL;
  677. goto out_bad_arg;
  678. }
  679. ret = vmw_user_shader_alloc(dev_priv, buffer, size, offset,
  680. shader_type, num_input_sig,
  681. num_output_sig, tfile, shader_handle);
  682. out_bad_arg:
  683. vmw_bo_unreference(&buffer);
  684. drm_gem_object_put(&buffer->base.base);
  685. return ret;
  686. }
  687. /**
  688. * vmw_shader_id_ok - Check whether a compat shader user key and
  689. * shader type are within valid bounds.
  690. *
  691. * @user_key: User space id of the shader.
  692. * @shader_type: Shader type.
  693. *
  694. * Returns true if valid false if not.
  695. */
  696. static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type)
  697. {
  698. return user_key <= ((1 << 20) - 1) && (unsigned) shader_type < 16;
  699. }
  700. /**
  701. * vmw_shader_key - Compute a hash key suitable for a compat shader.
  702. *
  703. * @user_key: User space id of the shader.
  704. * @shader_type: Shader type.
  705. *
  706. * Returns a hash key suitable for a command buffer managed resource
  707. * manager hash table.
  708. */
  709. static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type)
  710. {
  711. return user_key | (shader_type << 20);
  712. }
  713. /**
  714. * vmw_shader_remove - Stage a compat shader for removal.
  715. *
  716. * @man: Pointer to the compat shader manager identifying the shader namespace.
  717. * @user_key: The key that is used to identify the shader. The key is
  718. * unique to the shader type.
  719. * @shader_type: Shader type.
  720. * @list: Caller's list of staged command buffer resource actions.
  721. */
  722. int vmw_shader_remove(struct vmw_cmdbuf_res_manager *man,
  723. u32 user_key, SVGA3dShaderType shader_type,
  724. struct list_head *list)
  725. {
  726. struct vmw_resource *dummy;
  727. if (!vmw_shader_id_ok(user_key, shader_type))
  728. return -EINVAL;
  729. return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_shader,
  730. vmw_shader_key(user_key, shader_type),
  731. list, &dummy);
  732. }
  733. /**
  734. * vmw_compat_shader_add - Create a compat shader and stage it for addition
  735. * as a command buffer managed resource.
  736. *
  737. * @dev_priv: Pointer to device private structure.
  738. * @man: Pointer to the compat shader manager identifying the shader namespace.
  739. * @user_key: The key that is used to identify the shader. The key is
  740. * unique to the shader type.
  741. * @bytecode: Pointer to the bytecode of the shader.
  742. * @shader_type: Shader type.
  743. * @size: Command size.
  744. * @list: Caller's list of staged command buffer resource actions.
  745. *
  746. */
  747. int vmw_compat_shader_add(struct vmw_private *dev_priv,
  748. struct vmw_cmdbuf_res_manager *man,
  749. u32 user_key, const void *bytecode,
  750. SVGA3dShaderType shader_type,
  751. size_t size,
  752. struct list_head *list)
  753. {
  754. struct ttm_operation_ctx ctx = { false, true };
  755. struct vmw_buffer_object *buf;
  756. struct ttm_bo_kmap_obj map;
  757. bool is_iomem;
  758. int ret;
  759. struct vmw_resource *res;
  760. if (!vmw_shader_id_ok(user_key, shader_type))
  761. return -EINVAL;
  762. ret = vmw_bo_create(dev_priv, size, &vmw_sys_placement,
  763. true, true, vmw_bo_bo_free, &buf);
  764. if (unlikely(ret != 0))
  765. goto out;
  766. ret = ttm_bo_reserve(&buf->base, false, true, NULL);
  767. if (unlikely(ret != 0))
  768. goto no_reserve;
  769. /* Map and copy shader bytecode. */
  770. ret = ttm_bo_kmap(&buf->base, 0, PFN_UP(size), &map);
  771. if (unlikely(ret != 0)) {
  772. ttm_bo_unreserve(&buf->base);
  773. goto no_reserve;
  774. }
  775. memcpy(ttm_kmap_obj_virtual(&map, &is_iomem), bytecode, size);
  776. WARN_ON(is_iomem);
  777. ttm_bo_kunmap(&map);
  778. ret = ttm_bo_validate(&buf->base, &vmw_sys_placement, &ctx);
  779. WARN_ON(ret != 0);
  780. ttm_bo_unreserve(&buf->base);
  781. res = vmw_shader_alloc(dev_priv, buf, size, 0, shader_type);
  782. if (unlikely(ret != 0))
  783. goto no_reserve;
  784. ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader,
  785. vmw_shader_key(user_key, shader_type),
  786. res, list);
  787. vmw_resource_unreference(&res);
  788. no_reserve:
  789. vmw_bo_unreference(&buf);
  790. out:
  791. return ret;
  792. }
  793. /**
  794. * vmw_shader_lookup - Look up a compat shader
  795. *
  796. * @man: Pointer to the command buffer managed resource manager identifying
  797. * the shader namespace.
  798. * @user_key: The user space id of the shader.
  799. * @shader_type: The shader type.
  800. *
  801. * Returns a refcounted pointer to a struct vmw_resource if the shader was
  802. * found. An error pointer otherwise.
  803. */
  804. struct vmw_resource *
  805. vmw_shader_lookup(struct vmw_cmdbuf_res_manager *man,
  806. u32 user_key,
  807. SVGA3dShaderType shader_type)
  808. {
  809. if (!vmw_shader_id_ok(user_key, shader_type))
  810. return ERR_PTR(-EINVAL);
  811. return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_shader,
  812. vmw_shader_key(user_key, shader_type));
  813. }
  814. int vmw_shader_define_ioctl(struct drm_device *dev, void *data,
  815. struct drm_file *file_priv)
  816. {
  817. struct drm_vmw_shader_create_arg *arg =
  818. (struct drm_vmw_shader_create_arg *)data;
  819. return vmw_shader_define(dev, file_priv, arg->shader_type,
  820. arg->buffer_handle,
  821. arg->size, arg->offset,
  822. 0, 0,
  823. &arg->shader_handle);
  824. }