job.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Tegra host1x Job
  4. *
  5. * Copyright (c) 2010-2015, NVIDIA Corporation.
  6. */
  7. #include <linux/dma-mapping.h>
  8. #include <linux/err.h>
  9. #include <linux/host1x.h>
  10. #include <linux/iommu.h>
  11. #include <linux/kref.h>
  12. #include <linux/module.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/slab.h>
  15. #include <linux/vmalloc.h>
  16. #include <trace/events/host1x.h>
  17. #include "channel.h"
  18. #include "dev.h"
  19. #include "job.h"
  20. #include "syncpt.h"
  21. #define HOST1X_WAIT_SYNCPT_OFFSET 0x8
  22. struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
  23. u32 num_cmdbufs, u32 num_relocs,
  24. bool skip_firewall)
  25. {
  26. struct host1x_job *job = NULL;
  27. unsigned int num_unpins = num_relocs;
  28. bool enable_firewall;
  29. u64 total;
  30. void *mem;
  31. enable_firewall = IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && !skip_firewall;
  32. if (!enable_firewall)
  33. num_unpins += num_cmdbufs;
  34. /* Check that we're not going to overflow */
  35. total = sizeof(struct host1x_job) +
  36. (u64)num_relocs * sizeof(struct host1x_reloc) +
  37. (u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
  38. (u64)num_cmdbufs * sizeof(struct host1x_job_cmd) +
  39. (u64)num_unpins * sizeof(dma_addr_t) +
  40. (u64)num_unpins * sizeof(u32 *);
  41. if (total > ULONG_MAX)
  42. return NULL;
  43. mem = job = kzalloc(total, GFP_KERNEL);
  44. if (!job)
  45. return NULL;
  46. job->enable_firewall = enable_firewall;
  47. kref_init(&job->ref);
  48. job->channel = ch;
  49. /* Redistribute memory to the structs */
  50. mem += sizeof(struct host1x_job);
  51. job->relocs = num_relocs ? mem : NULL;
  52. mem += num_relocs * sizeof(struct host1x_reloc);
  53. job->unpins = num_unpins ? mem : NULL;
  54. mem += num_unpins * sizeof(struct host1x_job_unpin_data);
  55. job->cmds = num_cmdbufs ? mem : NULL;
  56. mem += num_cmdbufs * sizeof(struct host1x_job_cmd);
  57. job->addr_phys = num_unpins ? mem : NULL;
  58. job->reloc_addr_phys = job->addr_phys;
  59. job->gather_addr_phys = &job->addr_phys[num_relocs];
  60. return job;
  61. }
  62. EXPORT_SYMBOL(host1x_job_alloc);
  63. struct host1x_job *host1x_job_get(struct host1x_job *job)
  64. {
  65. kref_get(&job->ref);
  66. return job;
  67. }
  68. EXPORT_SYMBOL(host1x_job_get);
  69. static void job_free(struct kref *ref)
  70. {
  71. struct host1x_job *job = container_of(ref, struct host1x_job, ref);
  72. if (job->release)
  73. job->release(job);
  74. if (job->waiter)
  75. host1x_intr_put_ref(job->syncpt->host, job->syncpt->id,
  76. job->waiter, false);
  77. if (job->syncpt)
  78. host1x_syncpt_put(job->syncpt);
  79. kfree(job);
  80. }
  81. void host1x_job_put(struct host1x_job *job)
  82. {
  83. kref_put(&job->ref, job_free);
  84. }
  85. EXPORT_SYMBOL(host1x_job_put);
  86. void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
  87. unsigned int words, unsigned int offset)
  88. {
  89. struct host1x_job_gather *gather = &job->cmds[job->num_cmds].gather;
  90. gather->words = words;
  91. gather->bo = bo;
  92. gather->offset = offset;
  93. job->num_cmds++;
  94. }
  95. EXPORT_SYMBOL(host1x_job_add_gather);
  96. void host1x_job_add_wait(struct host1x_job *job, u32 id, u32 thresh,
  97. bool relative, u32 next_class)
  98. {
  99. struct host1x_job_cmd *cmd = &job->cmds[job->num_cmds];
  100. cmd->is_wait = true;
  101. cmd->wait.id = id;
  102. cmd->wait.threshold = thresh;
  103. cmd->wait.next_class = next_class;
  104. cmd->wait.relative = relative;
  105. job->num_cmds++;
  106. }
  107. EXPORT_SYMBOL(host1x_job_add_wait);
  108. static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
  109. {
  110. unsigned long mask = HOST1X_RELOC_READ | HOST1X_RELOC_WRITE;
  111. struct host1x_client *client = job->client;
  112. struct device *dev = client->dev;
  113. struct host1x_job_gather *g;
  114. unsigned int i;
  115. int err;
  116. job->num_unpins = 0;
  117. for (i = 0; i < job->num_relocs; i++) {
  118. struct host1x_reloc *reloc = &job->relocs[i];
  119. enum dma_data_direction direction;
  120. struct host1x_bo_mapping *map;
  121. struct host1x_bo *bo;
  122. reloc->target.bo = host1x_bo_get(reloc->target.bo);
  123. if (!reloc->target.bo) {
  124. err = -EINVAL;
  125. goto unpin;
  126. }
  127. bo = reloc->target.bo;
  128. switch (reloc->flags & mask) {
  129. case HOST1X_RELOC_READ:
  130. direction = DMA_TO_DEVICE;
  131. break;
  132. case HOST1X_RELOC_WRITE:
  133. direction = DMA_FROM_DEVICE;
  134. break;
  135. case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
  136. direction = DMA_BIDIRECTIONAL;
  137. break;
  138. default:
  139. err = -EINVAL;
  140. goto unpin;
  141. }
  142. map = host1x_bo_pin(dev, bo, direction, NULL);
  143. if (IS_ERR(map)) {
  144. err = PTR_ERR(map);
  145. goto unpin;
  146. }
  147. /*
  148. * host1x clients are generally not able to do scatter-gather themselves, so fail
  149. * if the buffer is discontiguous and we fail to map its SG table to a single
  150. * contiguous chunk of I/O virtual memory.
  151. */
  152. if (map->chunks > 1) {
  153. err = -EINVAL;
  154. goto unpin;
  155. }
  156. job->addr_phys[job->num_unpins] = map->phys;
  157. job->unpins[job->num_unpins].map = map;
  158. job->num_unpins++;
  159. }
  160. /*
  161. * We will copy gathers BO content later, so there is no need to
  162. * hold and pin them.
  163. */
  164. if (job->enable_firewall)
  165. return 0;
  166. for (i = 0; i < job->num_cmds; i++) {
  167. struct host1x_bo_mapping *map;
  168. size_t gather_size = 0;
  169. struct scatterlist *sg;
  170. unsigned long shift;
  171. struct iova *alloc;
  172. unsigned int j;
  173. if (job->cmds[i].is_wait)
  174. continue;
  175. g = &job->cmds[i].gather;
  176. g->bo = host1x_bo_get(g->bo);
  177. if (!g->bo) {
  178. err = -EINVAL;
  179. goto unpin;
  180. }
  181. map = host1x_bo_pin(host->dev, g->bo, DMA_TO_DEVICE, NULL);
  182. if (IS_ERR(map)) {
  183. err = PTR_ERR(map);
  184. goto unpin;
  185. }
  186. if (host->domain) {
  187. for_each_sgtable_sg(map->sgt, sg, j)
  188. gather_size += sg->length;
  189. gather_size = iova_align(&host->iova, gather_size);
  190. shift = iova_shift(&host->iova);
  191. alloc = alloc_iova(&host->iova, gather_size >> shift,
  192. host->iova_end >> shift, true);
  193. if (!alloc) {
  194. err = -ENOMEM;
  195. goto put;
  196. }
  197. err = iommu_map_sgtable(host->domain, iova_dma_addr(&host->iova, alloc),
  198. map->sgt, IOMMU_READ);
  199. if (err == 0) {
  200. __free_iova(&host->iova, alloc);
  201. err = -EINVAL;
  202. goto put;
  203. }
  204. map->phys = iova_dma_addr(&host->iova, alloc);
  205. map->size = gather_size;
  206. }
  207. job->addr_phys[job->num_unpins] = map->phys;
  208. job->unpins[job->num_unpins].map = map;
  209. job->num_unpins++;
  210. job->gather_addr_phys[i] = map->phys;
  211. }
  212. return 0;
  213. put:
  214. host1x_bo_put(g->bo);
  215. unpin:
  216. host1x_job_unpin(job);
  217. return err;
  218. }
  219. static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
  220. {
  221. void *cmdbuf_addr = NULL;
  222. struct host1x_bo *cmdbuf = g->bo;
  223. unsigned int i;
  224. /* pin & patch the relocs for one gather */
  225. for (i = 0; i < job->num_relocs; i++) {
  226. struct host1x_reloc *reloc = &job->relocs[i];
  227. u32 reloc_addr = (job->reloc_addr_phys[i] +
  228. reloc->target.offset) >> reloc->shift;
  229. u32 *target;
  230. /* skip all other gathers */
  231. if (cmdbuf != reloc->cmdbuf.bo)
  232. continue;
  233. if (job->enable_firewall) {
  234. target = (u32 *)job->gather_copy_mapped +
  235. reloc->cmdbuf.offset / sizeof(u32) +
  236. g->offset / sizeof(u32);
  237. goto patch_reloc;
  238. }
  239. if (!cmdbuf_addr) {
  240. cmdbuf_addr = host1x_bo_mmap(cmdbuf);
  241. if (unlikely(!cmdbuf_addr)) {
  242. pr_err("Could not map cmdbuf for relocation\n");
  243. return -ENOMEM;
  244. }
  245. }
  246. target = cmdbuf_addr + reloc->cmdbuf.offset;
  247. patch_reloc:
  248. *target = reloc_addr;
  249. }
  250. if (cmdbuf_addr)
  251. host1x_bo_munmap(cmdbuf, cmdbuf_addr);
  252. return 0;
  253. }
  254. static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
  255. unsigned int offset)
  256. {
  257. offset *= sizeof(u32);
  258. if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
  259. return false;
  260. /* relocation shift value validation isn't implemented yet */
  261. if (reloc->shift)
  262. return false;
  263. return true;
  264. }
  265. struct host1x_firewall {
  266. struct host1x_job *job;
  267. struct device *dev;
  268. unsigned int num_relocs;
  269. struct host1x_reloc *reloc;
  270. struct host1x_bo *cmdbuf;
  271. unsigned int offset;
  272. u32 words;
  273. u32 class;
  274. u32 reg;
  275. u32 mask;
  276. u32 count;
  277. };
  278. static int check_register(struct host1x_firewall *fw, unsigned long offset)
  279. {
  280. if (!fw->job->is_addr_reg)
  281. return 0;
  282. if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
  283. if (!fw->num_relocs)
  284. return -EINVAL;
  285. if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
  286. return -EINVAL;
  287. fw->num_relocs--;
  288. fw->reloc++;
  289. }
  290. return 0;
  291. }
  292. static int check_class(struct host1x_firewall *fw, u32 class)
  293. {
  294. if (!fw->job->is_valid_class) {
  295. if (fw->class != class)
  296. return -EINVAL;
  297. } else {
  298. if (!fw->job->is_valid_class(fw->class))
  299. return -EINVAL;
  300. }
  301. return 0;
  302. }
  303. static int check_mask(struct host1x_firewall *fw)
  304. {
  305. u32 mask = fw->mask;
  306. u32 reg = fw->reg;
  307. int ret;
  308. while (mask) {
  309. if (fw->words == 0)
  310. return -EINVAL;
  311. if (mask & 1) {
  312. ret = check_register(fw, reg);
  313. if (ret < 0)
  314. return ret;
  315. fw->words--;
  316. fw->offset++;
  317. }
  318. mask >>= 1;
  319. reg++;
  320. }
  321. return 0;
  322. }
  323. static int check_incr(struct host1x_firewall *fw)
  324. {
  325. u32 count = fw->count;
  326. u32 reg = fw->reg;
  327. int ret;
  328. while (count) {
  329. if (fw->words == 0)
  330. return -EINVAL;
  331. ret = check_register(fw, reg);
  332. if (ret < 0)
  333. return ret;
  334. reg++;
  335. fw->words--;
  336. fw->offset++;
  337. count--;
  338. }
  339. return 0;
  340. }
  341. static int check_nonincr(struct host1x_firewall *fw)
  342. {
  343. u32 count = fw->count;
  344. int ret;
  345. while (count) {
  346. if (fw->words == 0)
  347. return -EINVAL;
  348. ret = check_register(fw, fw->reg);
  349. if (ret < 0)
  350. return ret;
  351. fw->words--;
  352. fw->offset++;
  353. count--;
  354. }
  355. return 0;
  356. }
  357. static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
  358. {
  359. u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
  360. (g->offset / sizeof(u32));
  361. u32 job_class = fw->class;
  362. int err = 0;
  363. fw->words = g->words;
  364. fw->cmdbuf = g->bo;
  365. fw->offset = 0;
  366. while (fw->words && !err) {
  367. u32 word = cmdbuf_base[fw->offset];
  368. u32 opcode = (word & 0xf0000000) >> 28;
  369. fw->mask = 0;
  370. fw->reg = 0;
  371. fw->count = 0;
  372. fw->words--;
  373. fw->offset++;
  374. switch (opcode) {
  375. case 0:
  376. fw->class = word >> 6 & 0x3ff;
  377. fw->mask = word & 0x3f;
  378. fw->reg = word >> 16 & 0xfff;
  379. err = check_class(fw, job_class);
  380. if (!err)
  381. err = check_mask(fw);
  382. if (err)
  383. goto out;
  384. break;
  385. case 1:
  386. fw->reg = word >> 16 & 0xfff;
  387. fw->count = word & 0xffff;
  388. err = check_incr(fw);
  389. if (err)
  390. goto out;
  391. break;
  392. case 2:
  393. fw->reg = word >> 16 & 0xfff;
  394. fw->count = word & 0xffff;
  395. err = check_nonincr(fw);
  396. if (err)
  397. goto out;
  398. break;
  399. case 3:
  400. fw->mask = word & 0xffff;
  401. fw->reg = word >> 16 & 0xfff;
  402. err = check_mask(fw);
  403. if (err)
  404. goto out;
  405. break;
  406. case 4:
  407. case 14:
  408. break;
  409. default:
  410. err = -EINVAL;
  411. break;
  412. }
  413. }
  414. out:
  415. return err;
  416. }
  417. static inline int copy_gathers(struct device *host, struct host1x_job *job,
  418. struct device *dev)
  419. {
  420. struct host1x_firewall fw;
  421. size_t size = 0;
  422. size_t offset = 0;
  423. unsigned int i;
  424. fw.job = job;
  425. fw.dev = dev;
  426. fw.reloc = job->relocs;
  427. fw.num_relocs = job->num_relocs;
  428. fw.class = job->class;
  429. for (i = 0; i < job->num_cmds; i++) {
  430. struct host1x_job_gather *g;
  431. if (job->cmds[i].is_wait)
  432. continue;
  433. g = &job->cmds[i].gather;
  434. size += g->words * sizeof(u32);
  435. }
  436. /*
  437. * Try a non-blocking allocation from a higher priority pools first,
  438. * as awaiting for the allocation here is a major performance hit.
  439. */
  440. job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy,
  441. GFP_NOWAIT);
  442. /* the higher priority allocation failed, try the generic-blocking */
  443. if (!job->gather_copy_mapped)
  444. job->gather_copy_mapped = dma_alloc_wc(host, size,
  445. &job->gather_copy,
  446. GFP_KERNEL);
  447. if (!job->gather_copy_mapped)
  448. return -ENOMEM;
  449. job->gather_copy_size = size;
  450. for (i = 0; i < job->num_cmds; i++) {
  451. struct host1x_job_gather *g;
  452. void *gather;
  453. if (job->cmds[i].is_wait)
  454. continue;
  455. g = &job->cmds[i].gather;
  456. /* Copy the gather */
  457. gather = host1x_bo_mmap(g->bo);
  458. memcpy(job->gather_copy_mapped + offset, gather + g->offset,
  459. g->words * sizeof(u32));
  460. host1x_bo_munmap(g->bo, gather);
  461. /* Store the location in the buffer */
  462. g->base = job->gather_copy;
  463. g->offset = offset;
  464. /* Validate the job */
  465. if (validate(&fw, g))
  466. return -EINVAL;
  467. offset += g->words * sizeof(u32);
  468. }
  469. /* No relocs should remain at this point */
  470. if (fw.num_relocs)
  471. return -EINVAL;
  472. return 0;
  473. }
  474. int host1x_job_pin(struct host1x_job *job, struct device *dev)
  475. {
  476. int err;
  477. unsigned int i, j;
  478. struct host1x *host = dev_get_drvdata(dev->parent);
  479. /* pin memory */
  480. err = pin_job(host, job);
  481. if (err)
  482. goto out;
  483. if (job->enable_firewall) {
  484. err = copy_gathers(host->dev, job, dev);
  485. if (err)
  486. goto out;
  487. }
  488. /* patch gathers */
  489. for (i = 0; i < job->num_cmds; i++) {
  490. struct host1x_job_gather *g;
  491. if (job->cmds[i].is_wait)
  492. continue;
  493. g = &job->cmds[i].gather;
  494. /* process each gather mem only once */
  495. if (g->handled)
  496. continue;
  497. /* copy_gathers() sets gathers base if firewall is enabled */
  498. if (!job->enable_firewall)
  499. g->base = job->gather_addr_phys[i];
  500. for (j = i + 1; j < job->num_cmds; j++) {
  501. if (!job->cmds[j].is_wait &&
  502. job->cmds[j].gather.bo == g->bo) {
  503. job->cmds[j].gather.handled = true;
  504. job->cmds[j].gather.base = g->base;
  505. }
  506. }
  507. err = do_relocs(job, g);
  508. if (err)
  509. break;
  510. }
  511. out:
  512. if (err)
  513. host1x_job_unpin(job);
  514. wmb();
  515. return err;
  516. }
  517. EXPORT_SYMBOL(host1x_job_pin);
  518. void host1x_job_unpin(struct host1x_job *job)
  519. {
  520. struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
  521. unsigned int i;
  522. for (i = 0; i < job->num_unpins; i++) {
  523. struct host1x_bo_mapping *map = job->unpins[i].map;
  524. struct host1x_bo *bo = map->bo;
  525. if (!job->enable_firewall && map->size && host->domain) {
  526. iommu_unmap(host->domain, job->addr_phys[i], map->size);
  527. free_iova(&host->iova, iova_pfn(&host->iova, job->addr_phys[i]));
  528. }
  529. host1x_bo_unpin(map);
  530. host1x_bo_put(bo);
  531. }
  532. job->num_unpins = 0;
  533. if (job->gather_copy_size)
  534. dma_free_wc(host->dev, job->gather_copy_size,
  535. job->gather_copy_mapped, job->gather_copy);
  536. }
  537. EXPORT_SYMBOL(host1x_job_unpin);
  538. /*
  539. * Debug routine used to dump job entries
  540. */
  541. void host1x_job_dump(struct device *dev, struct host1x_job *job)
  542. {
  543. dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt->id);
  544. dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end);
  545. dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get);
  546. dev_dbg(dev, " TIMEOUT %d\n", job->timeout);
  547. dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots);
  548. dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins);
  549. }