job.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. * Tegra host1x Job
  3. *
  4. * Copyright (c) 2010-2013, NVIDIA Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/dma-mapping.h>
  19. #include <linux/err.h>
  20. #include <linux/kref.h>
  21. #include <linux/module.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <trace/events/host1x.h>
  26. #include "channel.h"
  27. #include "dev.h"
  28. #include "host1x_bo.h"
  29. #include "job.h"
  30. #include "syncpt.h"
  31. struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
  32. u32 num_cmdbufs, u32 num_relocs,
  33. u32 num_waitchks)
  34. {
  35. struct host1x_job *job = NULL;
  36. unsigned int num_unpins = num_cmdbufs + num_relocs;
  37. u64 total;
  38. void *mem;
  39. /* Check that we're not going to overflow */
  40. total = sizeof(struct host1x_job) +
  41. num_relocs * sizeof(struct host1x_reloc) +
  42. num_unpins * sizeof(struct host1x_job_unpin_data) +
  43. num_waitchks * sizeof(struct host1x_waitchk) +
  44. num_cmdbufs * sizeof(struct host1x_job_gather) +
  45. num_unpins * sizeof(dma_addr_t) +
  46. num_unpins * sizeof(u32 *);
  47. if (total > ULONG_MAX)
  48. return NULL;
  49. mem = job = kzalloc(total, GFP_KERNEL);
  50. if (!job)
  51. return NULL;
  52. kref_init(&job->ref);
  53. job->channel = ch;
  54. /* Redistribute memory to the structs */
  55. mem += sizeof(struct host1x_job);
  56. job->relocarray = num_relocs ? mem : NULL;
  57. mem += num_relocs * sizeof(struct host1x_reloc);
  58. job->unpins = num_unpins ? mem : NULL;
  59. mem += num_unpins * sizeof(struct host1x_job_unpin_data);
  60. job->waitchk = num_waitchks ? mem : NULL;
  61. mem += num_waitchks * sizeof(struct host1x_waitchk);
  62. job->gathers = num_cmdbufs ? mem : NULL;
  63. mem += num_cmdbufs * sizeof(struct host1x_job_gather);
  64. job->addr_phys = num_unpins ? mem : NULL;
  65. job->reloc_addr_phys = job->addr_phys;
  66. job->gather_addr_phys = &job->addr_phys[num_relocs];
  67. return job;
  68. }
  69. struct host1x_job *host1x_job_get(struct host1x_job *job)
  70. {
  71. kref_get(&job->ref);
  72. return job;
  73. }
  74. static void job_free(struct kref *ref)
  75. {
  76. struct host1x_job *job = container_of(ref, struct host1x_job, ref);
  77. kfree(job);
  78. }
  79. void host1x_job_put(struct host1x_job *job)
  80. {
  81. kref_put(&job->ref, job_free);
  82. }
  83. void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
  84. u32 words, u32 offset)
  85. {
  86. struct host1x_job_gather *cur_gather = &job->gathers[job->num_gathers];
  87. cur_gather->words = words;
  88. cur_gather->bo = bo;
  89. cur_gather->offset = offset;
  90. job->num_gathers++;
  91. }
  92. /*
  93. * NULL an already satisfied WAIT_SYNCPT host method, by patching its
  94. * args in the command stream. The method data is changed to reference
  95. * a reserved (never given out or incr) HOST1X_SYNCPT_RESERVED syncpt
  96. * with a matching threshold value of 0, so is guaranteed to be popped
  97. * by the host HW.
  98. */
  99. static void host1x_syncpt_patch_offset(struct host1x_syncpt *sp,
  100. struct host1x_bo *h, u32 offset)
  101. {
  102. void *patch_addr = NULL;
  103. /* patch the wait */
  104. patch_addr = host1x_bo_kmap(h, offset >> PAGE_SHIFT);
  105. if (patch_addr) {
  106. host1x_syncpt_patch_wait(sp,
  107. patch_addr + (offset & ~PAGE_MASK));
  108. host1x_bo_kunmap(h, offset >> PAGE_SHIFT, patch_addr);
  109. } else
  110. pr_err("Could not map cmdbuf for wait check\n");
  111. }
  112. /*
  113. * Check driver supplied waitchk structs for syncpt thresholds
  114. * that have already been satisfied and NULL the comparison (to
  115. * avoid a wrap condition in the HW).
  116. */
  117. static int do_waitchks(struct host1x_job *job, struct host1x *host,
  118. struct host1x_bo *patch)
  119. {
  120. int i;
  121. /* compare syncpt vs wait threshold */
  122. for (i = 0; i < job->num_waitchk; i++) {
  123. struct host1x_waitchk *wait = &job->waitchk[i];
  124. struct host1x_syncpt *sp =
  125. host1x_syncpt_get(host, wait->syncpt_id);
  126. /* validate syncpt id */
  127. if (wait->syncpt_id > host1x_syncpt_nb_pts(host))
  128. continue;
  129. /* skip all other gathers */
  130. if (patch != wait->bo)
  131. continue;
  132. trace_host1x_syncpt_wait_check(wait->bo, wait->offset,
  133. wait->syncpt_id, wait->thresh,
  134. host1x_syncpt_read_min(sp));
  135. if (host1x_syncpt_is_expired(sp, wait->thresh)) {
  136. dev_dbg(host->dev,
  137. "drop WAIT id %d (%s) thresh 0x%x, min 0x%x\n",
  138. wait->syncpt_id, sp->name, wait->thresh,
  139. host1x_syncpt_read_min(sp));
  140. host1x_syncpt_patch_offset(sp, patch, wait->offset);
  141. }
  142. wait->bo = NULL;
  143. }
  144. return 0;
  145. }
  146. static unsigned int pin_job(struct host1x_job *job)
  147. {
  148. unsigned int i;
  149. job->num_unpins = 0;
  150. for (i = 0; i < job->num_relocs; i++) {
  151. struct host1x_reloc *reloc = &job->relocarray[i];
  152. struct sg_table *sgt;
  153. dma_addr_t phys_addr;
  154. reloc->target = host1x_bo_get(reloc->target);
  155. if (!reloc->target)
  156. goto unpin;
  157. phys_addr = host1x_bo_pin(reloc->target, &sgt);
  158. if (!phys_addr)
  159. goto unpin;
  160. job->addr_phys[job->num_unpins] = phys_addr;
  161. job->unpins[job->num_unpins].bo = reloc->target;
  162. job->unpins[job->num_unpins].sgt = sgt;
  163. job->num_unpins++;
  164. }
  165. for (i = 0; i < job->num_gathers; i++) {
  166. struct host1x_job_gather *g = &job->gathers[i];
  167. struct sg_table *sgt;
  168. dma_addr_t phys_addr;
  169. g->bo = host1x_bo_get(g->bo);
  170. if (!g->bo)
  171. goto unpin;
  172. phys_addr = host1x_bo_pin(g->bo, &sgt);
  173. if (!phys_addr)
  174. goto unpin;
  175. job->addr_phys[job->num_unpins] = phys_addr;
  176. job->unpins[job->num_unpins].bo = g->bo;
  177. job->unpins[job->num_unpins].sgt = sgt;
  178. job->num_unpins++;
  179. }
  180. return job->num_unpins;
  181. unpin:
  182. host1x_job_unpin(job);
  183. return 0;
  184. }
  185. static unsigned int do_relocs(struct host1x_job *job, struct host1x_bo *cmdbuf)
  186. {
  187. int i = 0;
  188. u32 last_page = ~0;
  189. void *cmdbuf_page_addr = NULL;
  190. /* pin & patch the relocs for one gather */
  191. while (i < job->num_relocs) {
  192. struct host1x_reloc *reloc = &job->relocarray[i];
  193. u32 reloc_addr = (job->reloc_addr_phys[i] +
  194. reloc->target_offset) >> reloc->shift;
  195. u32 *target;
  196. /* skip all other gathers */
  197. if (!(reloc->cmdbuf && cmdbuf == reloc->cmdbuf)) {
  198. i++;
  199. continue;
  200. }
  201. if (last_page != reloc->cmdbuf_offset >> PAGE_SHIFT) {
  202. if (cmdbuf_page_addr)
  203. host1x_bo_kunmap(cmdbuf, last_page,
  204. cmdbuf_page_addr);
  205. cmdbuf_page_addr = host1x_bo_kmap(cmdbuf,
  206. reloc->cmdbuf_offset >> PAGE_SHIFT);
  207. last_page = reloc->cmdbuf_offset >> PAGE_SHIFT;
  208. if (unlikely(!cmdbuf_page_addr)) {
  209. pr_err("Could not map cmdbuf for relocation\n");
  210. return -ENOMEM;
  211. }
  212. }
  213. target = cmdbuf_page_addr + (reloc->cmdbuf_offset & ~PAGE_MASK);
  214. *target = reloc_addr;
  215. /* mark this gather as handled */
  216. reloc->cmdbuf = 0;
  217. }
  218. if (cmdbuf_page_addr)
  219. host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr);
  220. return 0;
  221. }
  222. static int check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
  223. unsigned int offset)
  224. {
  225. offset *= sizeof(u32);
  226. if (reloc->cmdbuf != cmdbuf || reloc->cmdbuf_offset != offset)
  227. return -EINVAL;
  228. return 0;
  229. }
  230. struct host1x_firewall {
  231. struct host1x_job *job;
  232. struct device *dev;
  233. unsigned int num_relocs;
  234. struct host1x_reloc *reloc;
  235. struct host1x_bo *cmdbuf_id;
  236. unsigned int offset;
  237. u32 words;
  238. u32 class;
  239. u32 reg;
  240. u32 mask;
  241. u32 count;
  242. };
  243. static int check_mask(struct host1x_firewall *fw)
  244. {
  245. u32 mask = fw->mask;
  246. u32 reg = fw->reg;
  247. while (mask) {
  248. if (fw->words == 0)
  249. return -EINVAL;
  250. if (mask & 1) {
  251. if (fw->job->is_addr_reg(fw->dev, fw->class, reg)) {
  252. bool bad_reloc = check_reloc(fw->reloc,
  253. fw->cmdbuf_id,
  254. fw->offset);
  255. if (!fw->num_relocs || bad_reloc)
  256. return -EINVAL;
  257. fw->reloc++;
  258. fw->num_relocs--;
  259. }
  260. fw->words--;
  261. fw->offset++;
  262. }
  263. mask >>= 1;
  264. reg++;
  265. }
  266. return 0;
  267. }
  268. static int check_incr(struct host1x_firewall *fw)
  269. {
  270. u32 count = fw->count;
  271. u32 reg = fw->reg;
  272. while (fw) {
  273. if (fw->words == 0)
  274. return -EINVAL;
  275. if (fw->job->is_addr_reg(fw->dev, fw->class, reg)) {
  276. bool bad_reloc = check_reloc(fw->reloc, fw->cmdbuf_id,
  277. fw->offset);
  278. if (!fw->num_relocs || bad_reloc)
  279. return -EINVAL;
  280. fw->reloc++;
  281. fw->num_relocs--;
  282. }
  283. reg++;
  284. fw->words--;
  285. fw->offset++;
  286. count--;
  287. }
  288. return 0;
  289. }
  290. static int check_nonincr(struct host1x_firewall *fw)
  291. {
  292. int is_addr_reg = fw->job->is_addr_reg(fw->dev, fw->class, fw->reg);
  293. u32 count = fw->count;
  294. while (count) {
  295. if (fw->words == 0)
  296. return -EINVAL;
  297. if (is_addr_reg) {
  298. bool bad_reloc = check_reloc(fw->reloc, fw->cmdbuf_id,
  299. fw->offset);
  300. if (!fw->num_relocs || bad_reloc)
  301. return -EINVAL;
  302. fw->reloc++;
  303. fw->num_relocs--;
  304. }
  305. fw->words--;
  306. fw->offset++;
  307. count--;
  308. }
  309. return 0;
  310. }
  311. static int validate(struct host1x_job *job, struct device *dev,
  312. struct host1x_job_gather *g)
  313. {
  314. u32 *cmdbuf_base;
  315. int err = 0;
  316. struct host1x_firewall fw;
  317. fw.job = job;
  318. fw.dev = dev;
  319. fw.reloc = job->relocarray;
  320. fw.num_relocs = job->num_relocs;
  321. fw.cmdbuf_id = g->bo;
  322. fw.offset = 0;
  323. fw.class = 0;
  324. if (!job->is_addr_reg)
  325. return 0;
  326. cmdbuf_base = host1x_bo_mmap(g->bo);
  327. if (!cmdbuf_base)
  328. return -ENOMEM;
  329. fw.words = g->words;
  330. while (fw.words && !err) {
  331. u32 word = cmdbuf_base[fw.offset];
  332. u32 opcode = (word & 0xf0000000) >> 28;
  333. fw.mask = 0;
  334. fw.reg = 0;
  335. fw.count = 0;
  336. fw.words--;
  337. fw.offset++;
  338. switch (opcode) {
  339. case 0:
  340. fw.class = word >> 6 & 0x3ff;
  341. fw.mask = word & 0x3f;
  342. fw.reg = word >> 16 & 0xfff;
  343. err = check_mask(&fw);
  344. if (err)
  345. goto out;
  346. break;
  347. case 1:
  348. fw.reg = word >> 16 & 0xfff;
  349. fw.count = word & 0xffff;
  350. err = check_incr(&fw);
  351. if (err)
  352. goto out;
  353. break;
  354. case 2:
  355. fw.reg = word >> 16 & 0xfff;
  356. fw.count = word & 0xffff;
  357. err = check_nonincr(&fw);
  358. if (err)
  359. goto out;
  360. break;
  361. case 3:
  362. fw.mask = word & 0xffff;
  363. fw.reg = word >> 16 & 0xfff;
  364. err = check_mask(&fw);
  365. if (err)
  366. goto out;
  367. break;
  368. case 4:
  369. case 5:
  370. case 14:
  371. break;
  372. default:
  373. err = -EINVAL;
  374. break;
  375. }
  376. }
  377. /* No relocs should remain at this point */
  378. if (fw.num_relocs)
  379. err = -EINVAL;
  380. out:
  381. host1x_bo_munmap(g->bo, cmdbuf_base);
  382. return err;
  383. }
  384. static inline int copy_gathers(struct host1x_job *job, struct device *dev)
  385. {
  386. size_t size = 0;
  387. size_t offset = 0;
  388. int i;
  389. for (i = 0; i < job->num_gathers; i++) {
  390. struct host1x_job_gather *g = &job->gathers[i];
  391. size += g->words * sizeof(u32);
  392. }
  393. job->gather_copy_mapped = dma_alloc_writecombine(dev, size,
  394. &job->gather_copy,
  395. GFP_KERNEL);
  396. if (!job->gather_copy_mapped) {
  397. int err = PTR_ERR(job->gather_copy_mapped);
  398. job->gather_copy_mapped = NULL;
  399. return err;
  400. }
  401. job->gather_copy_size = size;
  402. for (i = 0; i < job->num_gathers; i++) {
  403. struct host1x_job_gather *g = &job->gathers[i];
  404. void *gather;
  405. gather = host1x_bo_mmap(g->bo);
  406. memcpy(job->gather_copy_mapped + offset, gather + g->offset,
  407. g->words * sizeof(u32));
  408. host1x_bo_munmap(g->bo, gather);
  409. g->base = job->gather_copy;
  410. g->offset = offset;
  411. g->bo = NULL;
  412. offset += g->words * sizeof(u32);
  413. }
  414. return 0;
  415. }
  416. int host1x_job_pin(struct host1x_job *job, struct device *dev)
  417. {
  418. int err;
  419. unsigned int i, j;
  420. struct host1x *host = dev_get_drvdata(dev->parent);
  421. DECLARE_BITMAP(waitchk_mask, host1x_syncpt_nb_pts(host));
  422. bitmap_zero(waitchk_mask, host1x_syncpt_nb_pts(host));
  423. for (i = 0; i < job->num_waitchk; i++) {
  424. u32 syncpt_id = job->waitchk[i].syncpt_id;
  425. if (syncpt_id < host1x_syncpt_nb_pts(host))
  426. set_bit(syncpt_id, waitchk_mask);
  427. }
  428. /* get current syncpt values for waitchk */
  429. for_each_set_bit(i, waitchk_mask, host1x_syncpt_nb_pts(host))
  430. host1x_syncpt_load(host->syncpt + i);
  431. /* pin memory */
  432. err = pin_job(job);
  433. if (!err)
  434. goto out;
  435. /* patch gathers */
  436. for (i = 0; i < job->num_gathers; i++) {
  437. struct host1x_job_gather *g = &job->gathers[i];
  438. /* process each gather mem only once */
  439. if (g->handled)
  440. continue;
  441. g->base = job->gather_addr_phys[i];
  442. for (j = 0; j < job->num_gathers; j++)
  443. if (job->gathers[j].bo == g->bo)
  444. job->gathers[j].handled = true;
  445. err = 0;
  446. if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
  447. err = validate(job, dev, g);
  448. if (err)
  449. dev_err(dev, "Job invalid (err=%d)\n", err);
  450. if (!err)
  451. err = do_relocs(job, g->bo);
  452. if (!err)
  453. err = do_waitchks(job, host, g->bo);
  454. if (err)
  455. break;
  456. }
  457. if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && !err) {
  458. err = copy_gathers(job, dev);
  459. if (err) {
  460. host1x_job_unpin(job);
  461. return err;
  462. }
  463. }
  464. out:
  465. wmb();
  466. return err;
  467. }
  468. void host1x_job_unpin(struct host1x_job *job)
  469. {
  470. unsigned int i;
  471. for (i = 0; i < job->num_unpins; i++) {
  472. struct host1x_job_unpin_data *unpin = &job->unpins[i];
  473. host1x_bo_unpin(unpin->bo, unpin->sgt);
  474. host1x_bo_put(unpin->bo);
  475. }
  476. job->num_unpins = 0;
  477. if (job->gather_copy_size)
  478. dma_free_writecombine(job->channel->dev, job->gather_copy_size,
  479. job->gather_copy_mapped,
  480. job->gather_copy);
  481. }
  482. /*
  483. * Debug routine used to dump job entries
  484. */
  485. void host1x_job_dump(struct device *dev, struct host1x_job *job)
  486. {
  487. dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt_id);
  488. dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end);
  489. dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get);
  490. dev_dbg(dev, " TIMEOUT %d\n", job->timeout);
  491. dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots);
  492. dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins);
  493. }