keystone_remoteproc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /*
  2. * TI Keystone DSP remoteproc driver
  3. *
  4. * Copyright (C) 2015-2017 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms 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 that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/clk.h>
  18. #include <linux/slab.h>
  19. #include <linux/io.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_reserved_mem.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/regmap.h>
  29. #include <linux/mfd/syscon.h>
  30. #include <linux/remoteproc.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/uio_driver.h>
  33. #include <linux/reset.h>
  34. #include <uapi/linux/keystone_remoteproc.h>
  35. #include "remoteproc_internal.h"
  36. #define DRIVER_UIO_VERSION "0.1"
  37. #define KEYSTONE_RPROC_MAX_RSC_TABLE SZ_1K
  38. #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK (SZ_16M - 1)
  39. /*
  40. * XXX: evaluate if this param needs to be enhanced so that the switch between
  41. * userspace and remoteproc core loaders can be controlled per device.
  42. */
  43. static bool use_rproc_core_loader;
  44. module_param(use_rproc_core_loader, bool, 0444);
  45. /**
  46. * struct keystone_rproc_mem - internal memory structure
  47. * @cpu_addr: MPU virtual address of the memory region
  48. * @bus_addr: Bus address used to access the memory region
  49. * @dev_addr: Device address of the memory region from DSP view
  50. * @size: Size of the memory region
  51. * @kobj: kobject for the sysfs directory file
  52. */
  53. struct keystone_rproc_mem {
  54. void __iomem *cpu_addr;
  55. phys_addr_t bus_addr;
  56. u32 dev_addr;
  57. size_t size;
  58. struct kobject kobj;
  59. };
  60. /**
  61. * struct keystone_rproc - keystone remote processor driver structure
  62. * @dev: cached device pointer
  63. * @rproc: remoteproc device handle
  64. * @mem: internal memory regions data
  65. * @num_mems: number of internal memory regions
  66. * @dev_ctrl: device control regmap handle
  67. * @reset: reset control handle
  68. * @boot_offset: boot register offset in @dev_ctrl regmap
  69. * @irq_ring: irq entry for vring
  70. * @irq_fault: irq entry for exception
  71. * @kick_gpio: gpio used for virtio kicks
  72. * @workqueue: workqueue for processing virtio interrupts
  73. * @misc: misc device structure used to expose fops to user-space
  74. * @uio: uio device information
  75. * @mlock: lock to protect resources in fops
  76. * @lock: lock to protect shared resources within UIO interrupt handlers
  77. * @flags: flags to keep track of UIO interrupt occurrence
  78. * @rsc_table: resource table pointer copied from userspace
  79. * @rsc_table_size: size of resource table
  80. * @loaded_rsc_table: kernel pointer of loaded resource table
  81. * @boot_addr: remote processor boot address used with userspace loader
  82. * @open_count: fops open reference counter
  83. */
  84. struct keystone_rproc {
  85. struct device *dev;
  86. struct rproc *rproc;
  87. struct keystone_rproc_mem *mem;
  88. int num_mems;
  89. struct regmap *dev_ctrl;
  90. struct reset_control *reset;
  91. u32 boot_offset;
  92. int irq_ring;
  93. int irq_fault;
  94. int kick_gpio;
  95. struct work_struct workqueue;
  96. struct miscdevice misc;
  97. struct uio_info uio;
  98. struct mutex mlock; /* fops lock */
  99. spinlock_t lock; /* uio handler lock */
  100. unsigned long flags;
  101. struct resource_table *rsc_table;
  102. int rsc_table_size;
  103. void *loaded_rsc_table;
  104. u32 boot_addr;
  105. int open_count;
  106. };
  107. struct mem_sysfs_entry {
  108. struct attribute attr;
  109. ssize_t (*show)(struct keystone_rproc_mem *, char *);
  110. ssize_t (*store)(struct keystone_rproc_mem *, const char *, size_t);
  111. };
  112. static ssize_t mem_addr_show(struct keystone_rproc_mem *mem, char *buf)
  113. {
  114. return sprintf(buf, "%pa\n", &mem->bus_addr);
  115. }
  116. static ssize_t mem_size_show(struct keystone_rproc_mem *mem, char *buf)
  117. {
  118. return sprintf(buf, "0x%016zx\n", mem->size);
  119. }
  120. static struct mem_sysfs_entry addr_attribute =
  121. __ATTR(addr, 0444, mem_addr_show, NULL);
  122. static struct mem_sysfs_entry size_attribute =
  123. __ATTR(size, 0444, mem_size_show, NULL);
  124. static struct attribute *attrs[] = {
  125. &addr_attribute.attr,
  126. &size_attribute.attr,
  127. NULL, /* sentinel */
  128. };
  129. #define to_dsp_mem(m) container_of(m, struct keystone_rproc_mem, kobj)
  130. static ssize_t mem_type_show(struct kobject *kobj, struct attribute *attr,
  131. char *buf)
  132. {
  133. struct keystone_rproc_mem *mem = to_dsp_mem(kobj);
  134. struct mem_sysfs_entry *entry;
  135. entry = container_of(attr, struct mem_sysfs_entry, attr);
  136. if (!entry->show)
  137. return -EIO;
  138. return entry->show(mem, buf);
  139. }
  140. static const struct sysfs_ops mem_sysfs_ops = {
  141. .show = mem_type_show,
  142. };
  143. static struct kobj_type mem_attr_type = {
  144. .sysfs_ops = &mem_sysfs_ops,
  145. .default_attrs = attrs,
  146. };
  147. static int keystone_rproc_mem_add_attrs(struct keystone_rproc *ksproc)
  148. {
  149. int i, ret;
  150. struct keystone_rproc_mem *mem;
  151. struct kobject *kobj_parent = &ksproc->misc.this_device->kobj;
  152. for (i = 0; i < ksproc->num_mems; i++) {
  153. mem = &ksproc->mem[i];
  154. kobject_init(&mem->kobj, &mem_attr_type);
  155. ret = kobject_add(&mem->kobj, kobj_parent, "memory%d", i);
  156. if (ret)
  157. goto err_kobj;
  158. ret = kobject_uevent(&mem->kobj, KOBJ_ADD);
  159. if (ret)
  160. goto err_kobj;
  161. }
  162. return 0;
  163. err_kobj:
  164. for (; i >= 0; i--) {
  165. mem = &ksproc->mem[i];
  166. kobject_put(&mem->kobj);
  167. }
  168. return ret;
  169. }
  170. static void keystone_rproc_mem_del_attrs(struct keystone_rproc *ksproc)
  171. {
  172. int i;
  173. struct keystone_rproc_mem *mem;
  174. for (i = 0; i < ksproc->num_mems; i++) {
  175. mem = &ksproc->mem[i];
  176. kobject_put(&mem->kobj);
  177. }
  178. }
  179. static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len,
  180. u32 flags);
  181. /* uio handler dealing with userspace controlled exception interrupt */
  182. static irqreturn_t keystone_rproc_uio_handler(int irq, struct uio_info *uio)
  183. {
  184. struct keystone_rproc *ksproc = uio->priv;
  185. spin_lock(&ksproc->lock);
  186. if (!__test_and_set_bit(0, &ksproc->flags))
  187. disable_irq_nosync(irq);
  188. spin_unlock(&ksproc->lock);
  189. return IRQ_HANDLED;
  190. }
  191. /* uio driver interrupt control dealing with exception interrupt */
  192. static int keystone_rproc_uio_irqcontrol(struct uio_info *uio, s32 irq_on)
  193. {
  194. struct keystone_rproc *ksproc = uio->priv;
  195. unsigned long flags;
  196. spin_lock_irqsave(&ksproc->lock, flags);
  197. if (irq_on) {
  198. if (__test_and_clear_bit(0, &ksproc->flags))
  199. enable_irq(uio->irq);
  200. } else {
  201. if (!__test_and_set_bit(0, &ksproc->flags))
  202. disable_irq(uio->irq);
  203. }
  204. spin_unlock_irqrestore(&ksproc->lock, flags);
  205. return 0;
  206. }
  207. /* Reset previously set rsc table variables */
  208. static void keystone_rproc_reset_rsc_table(struct keystone_rproc *ksproc)
  209. {
  210. kfree(ksproc->rsc_table);
  211. ksproc->rsc_table = NULL;
  212. ksproc->loaded_rsc_table = NULL;
  213. ksproc->rsc_table_size = 0;
  214. }
  215. /*
  216. * Create/delete the virtio devices in kernel once the user-space loading is
  217. * complete, configure the remoteproc states appropriately, and boot or reset
  218. * the remote processor. The resource table should have been published through
  219. * KEYSTONE_RPROC_IOC_SET_RSC_TABLE & KEYSTONE_RPROC_IOC_SET_LOADED_RSC_TABLE
  220. * ioctls before invoking this. The boot address is passed through the
  221. * KEYSTONE_RPROC_IOC_SET_STATE ioctl when setting the KEYSTONE_RPROC_RUNNING
  222. * state.
  223. *
  224. * NOTE:
  225. * The ioctls KEYSTONE_RPROC_IOC_DSP_RESET and KEYSTONE_RPROC_IOC_DSP_BOOT
  226. * are restricted to support the booting or resetting the DSP devices only
  227. * for firmware images without any resource table.
  228. */
  229. static int keystone_rproc_set_state(struct keystone_rproc *ksproc,
  230. void __user *argp)
  231. {
  232. struct rproc *rproc = ksproc->rproc;
  233. struct keystone_rproc_set_state_params set_state_params;
  234. int ret = 0;
  235. if (copy_from_user(&set_state_params, argp, sizeof(set_state_params)))
  236. return -EFAULT;
  237. switch (set_state_params.state) {
  238. case KEYSTONE_RPROC_RUNNING:
  239. if (!ksproc->rsc_table || !ksproc->loaded_rsc_table)
  240. return -EINVAL;
  241. /*
  242. * store boot address for .get_boot_addr() rproc fw ops
  243. * XXX: validate the boot address so it is not set to a
  244. * random address
  245. */
  246. ksproc->boot_addr = set_state_params.boot_addr;
  247. /*
  248. * invoke rproc_boot to trigger the boot, the resource table
  249. * is parsed during the process and is agnostic of the presence
  250. * or absence of virtio devices
  251. */
  252. ret = rproc_boot(rproc);
  253. break;
  254. case KEYSTONE_RPROC_OFFLINE:
  255. if (rproc->state != RPROC_RUNNING)
  256. return -EINVAL;
  257. /* invoke rproc_shutdown to match rproc_boot */
  258. rproc_shutdown(rproc);
  259. mutex_lock(&ksproc->mlock);
  260. keystone_rproc_reset_rsc_table(ksproc);
  261. mutex_unlock(&ksproc->mlock);
  262. break;
  263. default:
  264. ret = -ENOTSUPP;
  265. }
  266. return ret;
  267. }
  268. /* Copy the resource table from userspace into kernel */
  269. static int keystone_rproc_set_rsc_table(struct keystone_rproc *ksproc,
  270. void __user *data)
  271. {
  272. unsigned long len = 0;
  273. void *rsc_table = NULL;
  274. if (!data)
  275. return -EFAULT;
  276. if (copy_from_user(&len, data, sizeof(len)))
  277. return -EFAULT;
  278. if (len >= KEYSTONE_RPROC_MAX_RSC_TABLE)
  279. return -EOVERFLOW;
  280. data += sizeof(len);
  281. rsc_table = kzalloc(len, GFP_KERNEL);
  282. if (!rsc_table)
  283. return -ENOMEM;
  284. if (copy_from_user(rsc_table, data, len))
  285. goto error_return;
  286. mutex_lock(&ksproc->mlock);
  287. kfree(ksproc->rsc_table);
  288. ksproc->rsc_table = rsc_table;
  289. ksproc->rsc_table_size = len;
  290. ksproc->loaded_rsc_table = NULL;
  291. mutex_unlock(&ksproc->mlock);
  292. return 0;
  293. error_return:
  294. kfree(rsc_table);
  295. return -EFAULT;
  296. }
  297. /*
  298. * Store the equivalent kernel virtual address of the loaded resource table in
  299. * device memory. Userspace published the device address of the loaded resource
  300. * table.
  301. */
  302. static int keystone_rproc_set_loaded_rsc_table(struct keystone_rproc *ksproc,
  303. unsigned int dma_addr)
  304. {
  305. struct rproc *rproc = ksproc->rproc;
  306. void *ptr;
  307. if (!ksproc->rsc_table_size || !ksproc->rsc_table)
  308. return -EINVAL;
  309. ptr = keystone_rproc_da_to_va(rproc, dma_addr, ksproc->rsc_table_size,
  310. RPROC_FLAGS_NONE);
  311. if (!ptr)
  312. return -EINVAL;
  313. ksproc->loaded_rsc_table = ptr;
  314. return 0;
  315. }
  316. /* Put the DSP processor into reset */
  317. static void keystone_rproc_dsp_reset(struct keystone_rproc *ksproc)
  318. {
  319. reset_control_assert(ksproc->reset);
  320. }
  321. /* Configure the boot address and boot the DSP processor */
  322. static int keystone_rproc_dsp_boot(struct keystone_rproc *ksproc,
  323. uint32_t boot_addr)
  324. {
  325. int ret;
  326. if (boot_addr & (SZ_1K - 1)) {
  327. dev_err(ksproc->dev, "invalid boot address 0x%x, must be aligned on a 1KB boundary\n",
  328. boot_addr);
  329. return -EINVAL;
  330. }
  331. ret = regmap_write(ksproc->dev_ctrl, ksproc->boot_offset, boot_addr);
  332. if (ret) {
  333. dev_err(ksproc->dev, "regmap_write of boot address failed, status = %d\n",
  334. ret);
  335. return ret;
  336. }
  337. reset_control_deassert(ksproc->reset);
  338. return 0;
  339. }
  340. static long
  341. keystone_rproc_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  342. {
  343. struct miscdevice *misc = filp->private_data;
  344. struct keystone_rproc *ksproc =
  345. container_of(misc, struct keystone_rproc, misc);
  346. void __user *argp = (void __user *)arg;
  347. int ret = 0;
  348. dev_dbg(ksproc->dev, "%s: cmd 0x%.8x (%d), arg 0x%lx\n",
  349. __func__, cmd, _IOC_NR(cmd), arg);
  350. if (_IOC_TYPE(cmd) != KEYSTONE_RPROC_IOC_MAGIC)
  351. return -ENOTTY;
  352. if (_IOC_NR(cmd) >= KEYSTONE_RPROC_IOC_MAXNR)
  353. return -ENOTTY;
  354. switch (cmd) {
  355. case KEYSTONE_RPROC_IOC_SET_STATE:
  356. ret = keystone_rproc_set_state(ksproc, argp);
  357. break;
  358. case KEYSTONE_RPROC_IOC_SET_RSC_TABLE:
  359. ret = keystone_rproc_set_rsc_table(ksproc, argp);
  360. break;
  361. case KEYSTONE_RPROC_IOC_SET_LOADED_RSC_TABLE:
  362. ret = keystone_rproc_set_loaded_rsc_table(ksproc, arg);
  363. break;
  364. case KEYSTONE_RPROC_IOC_DSP_RESET:
  365. if (ksproc->rsc_table) {
  366. ret = -EINVAL;
  367. break;
  368. }
  369. keystone_rproc_dsp_reset(ksproc);
  370. break;
  371. case KEYSTONE_RPROC_IOC_DSP_BOOT:
  372. if (ksproc->rsc_table) {
  373. ret = -EINVAL;
  374. break;
  375. }
  376. ret = keystone_rproc_dsp_boot(ksproc, arg);
  377. break;
  378. default:
  379. ret = -ENOTTY;
  380. break;
  381. }
  382. if (ret) {
  383. dev_err(ksproc->dev, "error in ioctl call: cmd 0x%.8x (%d), ret %d\n",
  384. cmd, _IOC_NR(cmd), ret);
  385. }
  386. return ret;
  387. }
  388. /*
  389. * Map DSP memories into userspace for supporting Userspace loading.
  390. *
  391. * This is a custom mmap function following semantics based on the UIO
  392. * mmap implementation. The vm_pgoff passed in the vma structure is a
  393. * combination of the memory region index and the actual page offset in
  394. * that region. This checks if user request is in valid range before
  395. * providing mmap access.
  396. *
  397. * XXX: Evaluate this approach, as the internal memories can be mapped in
  398. * whole into userspace as they are not super-large, or switch to using
  399. * direct addresses to look more like a traditional implementation.
  400. */
  401. static int keystone_rproc_mmap(struct file *file, struct vm_area_struct *vma)
  402. {
  403. struct miscdevice *misc = file->private_data;
  404. struct keystone_rproc *ksproc =
  405. container_of(misc, struct keystone_rproc, misc);
  406. size_t size = vma->vm_end - vma->vm_start;
  407. size_t req_offset;
  408. u32 index;
  409. index = vma->vm_pgoff & KEYSTONE_RPROC_UIO_MAP_INDEX_MASK;
  410. if (index >= ksproc->num_mems) {
  411. dev_err(ksproc->dev, "invalid mmap region index %d\n", index);
  412. return -EINVAL;
  413. }
  414. req_offset = (vma->vm_pgoff - index) << PAGE_SHIFT;
  415. if (req_offset + size < req_offset) {
  416. dev_err(ksproc->dev, "invalid request - overflow, mmap offset = 0x%zx size 0x%zx region %d\n",
  417. req_offset, size, index);
  418. return -EINVAL;
  419. }
  420. if ((req_offset + size) > ksproc->mem[index].size) {
  421. dev_err(ksproc->dev, "invalid request - out of range, mmap offset 0x%zx size 0x%zx region %d\n",
  422. req_offset, size, index);
  423. return -EINVAL;
  424. }
  425. vma->vm_page_prot = phys_mem_access_prot(file,
  426. (ksproc->mem[index].bus_addr >> PAGE_SHIFT) +
  427. (vma->vm_pgoff - index), size,
  428. vma->vm_page_prot);
  429. if (remap_pfn_range(vma, vma->vm_start,
  430. (ksproc->mem[index].bus_addr >> PAGE_SHIFT) +
  431. (vma->vm_pgoff - index), size, vma->vm_page_prot))
  432. return -EAGAIN;
  433. return 0;
  434. }
  435. static int keystone_rproc_open(struct inode *inode, struct file *file)
  436. {
  437. struct miscdevice *misc = file->private_data;
  438. struct keystone_rproc *ksproc =
  439. container_of(misc, struct keystone_rproc, misc);
  440. mutex_lock(&ksproc->mlock);
  441. ksproc->open_count++;
  442. mutex_unlock(&ksproc->mlock);
  443. return 0;
  444. }
  445. static int keystone_rproc_release(struct inode *inode, struct file *filp)
  446. {
  447. struct miscdevice *misc = filp->private_data;
  448. struct keystone_rproc *ksproc =
  449. container_of(misc, struct keystone_rproc, misc);
  450. struct rproc *rproc = ksproc->rproc;
  451. mutex_lock(&ksproc->mlock);
  452. if ((WARN_ON(ksproc->open_count == 0)))
  453. goto end;
  454. if (--ksproc->open_count > 0)
  455. goto end;
  456. if (rproc->state != RPROC_OFFLINE) {
  457. rproc_shutdown(rproc);
  458. WARN_ON(rproc->state != RPROC_OFFLINE);
  459. }
  460. keystone_rproc_reset_rsc_table(ksproc);
  461. end:
  462. mutex_unlock(&ksproc->mlock);
  463. return 0;
  464. }
  465. /*
  466. * File operations exposed through a miscdevice for supporting
  467. * the userspace loader/boot mechanism.
  468. */
  469. static const struct file_operations keystone_rproc_fops = {
  470. .owner = THIS_MODULE,
  471. .unlocked_ioctl = keystone_rproc_ioctl,
  472. .mmap = keystone_rproc_mmap,
  473. .open = keystone_rproc_open,
  474. .release = keystone_rproc_release,
  475. };
  476. /*
  477. * Used only with userspace loader/boot mechanism, the parsing of the firmware
  478. * is done in userspace, and a copy of the resource table is added for the
  479. * kernel-level access through an ioctl. Return the pointer to this resource
  480. * table for the remoteproc core to process the resource table for creating
  481. * the vrings and traces.
  482. */
  483. static struct resource_table *
  484. keystone_rproc_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
  485. int *tablesz)
  486. {
  487. struct keystone_rproc *ksproc = rproc->priv;
  488. if (tablesz)
  489. *tablesz = ksproc->rsc_table_size;
  490. return ksproc->rsc_table;
  491. }
  492. /*
  493. * Used only with userspace loader/boot mechanism, the device address of the
  494. * loaded resource table is published to the kernel-level through an ioctl
  495. * at which point the equivalent kernel virtual pointer is stored in a local
  496. * variable in the keystone_rproc device structure. Return this kernel pointer
  497. * to the remoteproc core for runtime publishing/modification of the resource
  498. * table entries.
  499. *
  500. * NOTE: Only loaded resource tables in the DSP internal memories is supported
  501. * at present.
  502. */
  503. static struct resource_table *
  504. keystone_rproc_find_loaded_rsc_table(struct rproc *rproc,
  505. const struct firmware *fw)
  506. {
  507. struct keystone_rproc *ksproc = rproc->priv;
  508. return ksproc->loaded_rsc_table;
  509. }
  510. /*
  511. * Used only with userspace loader/boot mechanism, the boot address
  512. * is published to the kernel-level through an ioctl call and is
  513. * stored in a local variable in the keystone_rproc device structure.
  514. * Return this address to the remoteproc core through the .get_boot_addr()
  515. * remoteproc firmware ops
  516. */
  517. static u32 keystone_rproc_get_boot_addr(struct rproc *rproc,
  518. const struct firmware *fw)
  519. {
  520. struct keystone_rproc *ksproc = rproc->priv;
  521. return ksproc->boot_addr;
  522. }
  523. /*
  524. * Used only with userspace loader/boot mechanism, otherwise the remoteproc
  525. * core elf loader's functions are relied on.
  526. */
  527. static struct rproc_fw_ops keystone_rproc_fw_ops = {
  528. .find_rsc_table = keystone_rproc_find_rsc_table,
  529. .find_loaded_rsc_table = keystone_rproc_find_loaded_rsc_table,
  530. .get_boot_addr = keystone_rproc_get_boot_addr,
  531. };
  532. /*
  533. * Process the remoteproc exceptions
  534. *
  535. * The exception reporting on Keystone DSP remote processors is very simple
  536. * compared to the equivalent processors on the OMAP family, it is notified
  537. * through a software-designed specific interrupt source in the IPC interrupt
  538. * generation register.
  539. *
  540. * This function just invokes the rproc_report_crash to report the exception
  541. * to the remoteproc driver core, to trigger a recovery. This is the case
  542. * only when using in-kernel remoteproc core loader/boot mechanism, and is
  543. * handled through an UIO interrupt otherwise.
  544. */
  545. static irqreturn_t keystone_rproc_exception_interrupt(int irq, void *dev_id)
  546. {
  547. struct keystone_rproc *ksproc = dev_id;
  548. rproc_report_crash(ksproc->rproc, RPROC_FATAL_ERROR);
  549. return IRQ_HANDLED;
  550. }
  551. /*
  552. * Main virtqueue message workqueue function
  553. *
  554. * This function is executed upon scheduling of the keystone remoteproc
  555. * driver's workqueue. The workqueue is scheduled by the vring ISR handler.
  556. *
  557. * There is no payload message indicating the virtqueue index as is the
  558. * case with mailbox-based implementations on OMAP family. As such, this
  559. * handler processes both the Tx and Rx virtqueue indices on every invocation.
  560. * The rproc_vq_interrupt function can detect if there are new unprocessed
  561. * messages or not (returns IRQ_NONE vs IRQ_HANDLED), but there is no need
  562. * to check for these return values. The index 0 triggering will process all
  563. * pending Rx buffers, and the index 1 triggering will process all newly
  564. * available Tx buffers and will wakeup any potentially blocked senders.
  565. *
  566. * NOTE:
  567. * 1. A payload could be added by using some of the source bits in the
  568. * IPC interrupt generation registers, but this would need additional
  569. * changes to the overall IPC stack, and currently there are no benefits
  570. * of adapting that approach.
  571. * 2. The current logic is based on an inherent design assumption of supporting
  572. * only 2 vrings, but this can be changed if needed.
  573. */
  574. static void handle_event(struct work_struct *work)
  575. {
  576. struct keystone_rproc *ksproc =
  577. container_of(work, struct keystone_rproc, workqueue);
  578. rproc_vq_interrupt(ksproc->rproc, 0);
  579. rproc_vq_interrupt(ksproc->rproc, 1);
  580. }
  581. /*
  582. * Interrupt handler for processing vring kicks from remote processor
  583. */
  584. static irqreturn_t keystone_rproc_vring_interrupt(int irq, void *dev_id)
  585. {
  586. struct keystone_rproc *ksproc = dev_id;
  587. schedule_work(&ksproc->workqueue);
  588. return IRQ_HANDLED;
  589. }
  590. /*
  591. * Power up the DSP remote processor.
  592. *
  593. * This function will be invoked only after the firmware for this rproc
  594. * was loaded, parsed successfully, and all of its resource requirements
  595. * were met. The function skips releasing the processor from reset and
  596. * registering for the exception interrupt if using the userspace controlled
  597. * load/boot mechanism. The processor will be started through an ioctl when
  598. * controlled from userspace, but the virtio interrupt still is handled at
  599. * the kernel layer.
  600. */
  601. static int keystone_rproc_start(struct rproc *rproc)
  602. {
  603. struct keystone_rproc *ksproc = rproc->priv;
  604. int ret;
  605. INIT_WORK(&ksproc->workqueue, handle_event);
  606. ret = request_irq(ksproc->irq_ring, keystone_rproc_vring_interrupt, 0,
  607. dev_name(ksproc->dev), ksproc);
  608. if (ret) {
  609. dev_err(ksproc->dev, "failed to enable vring interrupt, ret = %d\n",
  610. ret);
  611. goto out;
  612. }
  613. if (!rproc->use_userspace_loader) {
  614. ret = request_irq(ksproc->irq_fault,
  615. keystone_rproc_exception_interrupt, 0,
  616. dev_name(ksproc->dev), ksproc);
  617. if (ret) {
  618. dev_err(ksproc->dev, "failed to enable exception interrupt, ret = %d\n",
  619. ret);
  620. goto free_vring_irq;
  621. }
  622. }
  623. ret = keystone_rproc_dsp_boot(ksproc, rproc->bootaddr);
  624. if (ret)
  625. goto free_exc_irq;
  626. return 0;
  627. free_exc_irq:
  628. free_irq(ksproc->irq_fault, ksproc);
  629. free_vring_irq:
  630. free_irq(ksproc->irq_ring, ksproc);
  631. flush_work(&ksproc->workqueue);
  632. out:
  633. return ret;
  634. }
  635. /*
  636. * Stop the DSP remote processor.
  637. *
  638. * This function puts the DSP processor into reset, and finishes processing
  639. * of any pending messages. The reset procedure is completed only if using
  640. * kernel-mode remoteproc loading/booting mechanism, it is handled outside
  641. * if using userspace load/boot mechanism either through an ioctl, or when
  642. * the handle to the device is closed without triggering a reset.
  643. */
  644. static int keystone_rproc_stop(struct rproc *rproc)
  645. {
  646. struct keystone_rproc *ksproc = rproc->priv;
  647. if (!rproc->use_userspace_loader) {
  648. keystone_rproc_dsp_reset(ksproc);
  649. free_irq(ksproc->irq_fault, ksproc);
  650. }
  651. free_irq(ksproc->irq_ring, ksproc);
  652. flush_work(&ksproc->workqueue);
  653. return 0;
  654. }
  655. /*
  656. * Kick the remote processor to notify about pending unprocessed messages.
  657. * The vqid usage is not used and is inconsequential, as the kick is performed
  658. * through a simulated GPIO (an bit in an IPC interrupt-triggering register),
  659. * the remote processor is expected to process both its Tx and Rx virtqueues.
  660. */
  661. static void keystone_rproc_kick(struct rproc *rproc, int vqid)
  662. {
  663. struct keystone_rproc *ksproc = rproc->priv;
  664. if (WARN_ON(ksproc->kick_gpio < 0))
  665. return;
  666. gpio_set_value(ksproc->kick_gpio, 1);
  667. }
  668. /*
  669. * Custom function to translate a DSP device address (internal RAMs only) to a
  670. * kernel virtual address. The DSPs can access their RAMs at either an internal
  671. * address visible only from a DSP, or at the SoC-level bus address. Both these
  672. * addresses need to be looked through for translation. The translated addresses
  673. * can be used either by the remoteproc core for loading (when using kernel
  674. * remoteproc loader), or by any rpmsg bus drivers.
  675. */
  676. static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, int len,
  677. u32 flags)
  678. {
  679. struct keystone_rproc *ksproc = rproc->priv;
  680. void __iomem *va = NULL;
  681. phys_addr_t bus_addr;
  682. u32 dev_addr, offset;
  683. size_t size;
  684. int i;
  685. if (len <= 0)
  686. return NULL;
  687. for (i = 0; i < ksproc->num_mems; i++) {
  688. bus_addr = ksproc->mem[i].bus_addr;
  689. dev_addr = ksproc->mem[i].dev_addr;
  690. size = ksproc->mem[i].size;
  691. if (da < KEYSTONE_RPROC_LOCAL_ADDRESS_MASK) {
  692. /* handle DSP-view addresses */
  693. if ((da >= dev_addr) &&
  694. ((da + len) <= (dev_addr + size))) {
  695. offset = da - dev_addr;
  696. va = ksproc->mem[i].cpu_addr + offset;
  697. break;
  698. }
  699. } else {
  700. /* handle SoC-view addresses */
  701. if ((da >= bus_addr) &&
  702. (da + len) <= (bus_addr + size)) {
  703. offset = da - bus_addr;
  704. va = ksproc->mem[i].cpu_addr + offset;
  705. break;
  706. }
  707. }
  708. }
  709. return (__force void *)va;
  710. }
  711. static const struct rproc_ops keystone_rproc_ops = {
  712. .start = keystone_rproc_start,
  713. .stop = keystone_rproc_stop,
  714. .kick = keystone_rproc_kick,
  715. .da_to_va = keystone_rproc_da_to_va,
  716. };
  717. static int keystone_rproc_of_get_memories(struct platform_device *pdev,
  718. struct keystone_rproc *ksproc)
  719. {
  720. static const char * const mem_names[] = {"l2sram", "l1pram", "l1dram"};
  721. struct device *dev = &pdev->dev;
  722. struct resource *res;
  723. int num_mems = 0;
  724. int i;
  725. num_mems = ARRAY_SIZE(mem_names);
  726. ksproc->mem = devm_kcalloc(ksproc->dev, num_mems,
  727. sizeof(*ksproc->mem), GFP_KERNEL);
  728. if (!ksproc->mem)
  729. return -ENOMEM;
  730. for (i = 0; i < num_mems; i++) {
  731. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  732. mem_names[i]);
  733. ksproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
  734. if (IS_ERR(ksproc->mem[i].cpu_addr)) {
  735. dev_err(dev, "failed to parse and map %s memory\n",
  736. mem_names[i]);
  737. return PTR_ERR(ksproc->mem[i].cpu_addr);
  738. }
  739. ksproc->mem[i].bus_addr = res->start;
  740. ksproc->mem[i].dev_addr =
  741. res->start & KEYSTONE_RPROC_LOCAL_ADDRESS_MASK;
  742. ksproc->mem[i].size = resource_size(res);
  743. /* zero out memories to start in a pristine state */
  744. memset((__force void *)ksproc->mem[i].cpu_addr, 0,
  745. ksproc->mem[i].size);
  746. }
  747. ksproc->num_mems = num_mems;
  748. return 0;
  749. }
  750. static int keystone_rproc_of_get_dev_syscon(struct platform_device *pdev,
  751. struct keystone_rproc *ksproc)
  752. {
  753. struct device_node *np = pdev->dev.of_node;
  754. struct device *dev = &pdev->dev;
  755. int ret;
  756. if (!of_property_read_bool(np, "ti,syscon-dev")) {
  757. dev_err(dev, "ti,syscon-dev property is absent\n");
  758. return -EINVAL;
  759. }
  760. ksproc->dev_ctrl =
  761. syscon_regmap_lookup_by_phandle(np, "ti,syscon-dev");
  762. if (IS_ERR(ksproc->dev_ctrl)) {
  763. ret = PTR_ERR(ksproc->dev_ctrl);
  764. return ret;
  765. }
  766. if (of_property_read_u32_index(np, "ti,syscon-dev", 1,
  767. &ksproc->boot_offset)) {
  768. dev_err(dev, "couldn't read the boot register offset\n");
  769. return -EINVAL;
  770. }
  771. return 0;
  772. }
  773. static int keystone_rproc_probe(struct platform_device *pdev)
  774. {
  775. struct device *dev = &pdev->dev;
  776. struct device_node *np = dev->of_node;
  777. struct keystone_rproc *ksproc;
  778. struct miscdevice *misc;
  779. struct uio_info *uio;
  780. struct rproc *rproc;
  781. const char *name;
  782. char *fw_name = NULL;
  783. char *template = "keystone-%s-fw";
  784. int name_len = 0;
  785. int ret = 0;
  786. if (!np) {
  787. dev_err(dev, "only DT-based devices are supported\n");
  788. return -ENODEV;
  789. }
  790. ret = of_property_read_string(np, "label", &name);
  791. if (ret < 0) {
  792. dev_err(dev, "failed to get label, status = %d\n", ret);
  793. return ret;
  794. }
  795. /* construct a custom default fw name - subject to change in future */
  796. name_len = strlen(name) + strlen(template) - 2 + 1;
  797. fw_name = devm_kzalloc(dev, name_len, GFP_KERNEL);
  798. if (!fw_name)
  799. return -ENOMEM;
  800. snprintf(fw_name, name_len, template, name);
  801. rproc = rproc_alloc(dev, dev_name(dev), &keystone_rproc_ops, fw_name,
  802. sizeof(*ksproc));
  803. if (!rproc)
  804. return -ENOMEM;
  805. rproc->has_iommu = false;
  806. rproc->use_userspace_loader = !use_rproc_core_loader;
  807. if (rproc->use_userspace_loader) {
  808. rproc->recovery_disabled = true;
  809. rproc->auto_boot = false;
  810. rproc->fw_ops = &keystone_rproc_fw_ops;
  811. }
  812. ksproc = rproc->priv;
  813. ksproc->rproc = rproc;
  814. ksproc->dev = dev;
  815. mutex_init(&ksproc->mlock);
  816. spin_lock_init(&ksproc->lock);
  817. ret = keystone_rproc_of_get_dev_syscon(pdev, ksproc);
  818. if (ret)
  819. goto free_rproc;
  820. ksproc->reset = devm_reset_control_get(dev, NULL);
  821. if (IS_ERR(ksproc->reset)) {
  822. ret = PTR_ERR(ksproc->reset);
  823. goto free_rproc;
  824. }
  825. /* enable clock for accessing DSP internal memories */
  826. pm_runtime_enable(dev);
  827. ret = pm_runtime_get_sync(dev);
  828. if (ret < 0) {
  829. dev_err(dev, "failed to enable clock, status = %d\n", ret);
  830. pm_runtime_put_noidle(dev);
  831. goto disable_rpm;
  832. }
  833. ret = keystone_rproc_of_get_memories(pdev, ksproc);
  834. if (ret)
  835. goto disable_clk;
  836. ksproc->irq_ring = platform_get_irq_byname(pdev, "vring");
  837. if (ksproc->irq_ring < 0) {
  838. ret = ksproc->irq_ring;
  839. dev_err(dev, "failed to get vring interrupt, status = %d\n",
  840. ret);
  841. goto disable_clk;
  842. }
  843. ksproc->irq_fault = platform_get_irq_byname(pdev, "exception");
  844. if (ksproc->irq_fault < 0) {
  845. ret = ksproc->irq_fault;
  846. dev_err(dev, "failed to get exception interrupt, status = %d\n",
  847. ret);
  848. goto disable_clk;
  849. }
  850. ksproc->kick_gpio = of_get_named_gpio_flags(np, "kick-gpio", 0, NULL);
  851. if (ksproc->kick_gpio < 0) {
  852. ret = ksproc->kick_gpio;
  853. dev_err(dev, "failed to get gpio for virtio kicks, status = %d\n",
  854. ret);
  855. goto disable_clk;
  856. }
  857. if (of_reserved_mem_device_init(dev))
  858. dev_warn(dev, "device does not have specific CMA pool\n");
  859. if (rproc_get_alias_id(rproc) < 0)
  860. dev_warn(&pdev->dev, "device does not have an alias id\n");
  861. /* ensure the DSP is in reset before loading firmware */
  862. ret = reset_control_status(ksproc->reset);
  863. if (ret < 0) {
  864. dev_err(dev, "failed to get reset status, status = %d\n", ret);
  865. goto release_mem;
  866. } else if (ret == 0) {
  867. WARN(1, "device is not in reset\n");
  868. keystone_rproc_dsp_reset(ksproc);
  869. }
  870. ret = rproc_add(rproc);
  871. if (ret) {
  872. dev_err(dev, "failed to add register device with remoteproc core, status = %d\n",
  873. ret);
  874. goto release_mem;
  875. }
  876. platform_set_drvdata(pdev, ksproc);
  877. if (rproc->use_userspace_loader) {
  878. uio = &ksproc->uio;
  879. uio->name = name;
  880. uio->version = DRIVER_UIO_VERSION;
  881. uio->irq = ksproc->irq_fault;
  882. uio->priv = ksproc;
  883. uio->handler = keystone_rproc_uio_handler;
  884. uio->irqcontrol = keystone_rproc_uio_irqcontrol;
  885. ret = uio_register_device(dev, uio);
  886. if (ret) {
  887. dev_err(dev, "failed to register uio device, status = %d\n",
  888. ret);
  889. goto del_rproc;
  890. }
  891. dev_dbg(dev, "registered uio device %s\n", uio->name);
  892. misc = &ksproc->misc;
  893. misc->minor = MISC_DYNAMIC_MINOR;
  894. misc->name = uio->name;
  895. misc->fops = &keystone_rproc_fops;
  896. misc->parent = dev;
  897. ret = misc_register(misc);
  898. if (ret) {
  899. dev_err(dev, "failed to register misc device, status = %d\n",
  900. ret);
  901. goto unregister_uio;
  902. }
  903. ret = keystone_rproc_mem_add_attrs(ksproc);
  904. if (ret) {
  905. dev_err(ksproc->dev, "error creating sysfs files (%d)\n",
  906. ret);
  907. goto unregister_misc;
  908. }
  909. dev_dbg(dev, "registered misc device %s\n", misc->name);
  910. }
  911. return 0;
  912. unregister_misc:
  913. misc_deregister(misc);
  914. unregister_uio:
  915. uio_unregister_device(uio);
  916. del_rproc:
  917. rproc_del(rproc);
  918. release_mem:
  919. of_reserved_mem_device_release(dev);
  920. disable_clk:
  921. pm_runtime_put_sync(dev);
  922. disable_rpm:
  923. pm_runtime_disable(dev);
  924. free_rproc:
  925. rproc_free(rproc);
  926. return ret;
  927. }
  928. static int keystone_rproc_remove(struct platform_device *pdev)
  929. {
  930. struct keystone_rproc *ksproc = platform_get_drvdata(pdev);
  931. struct rproc *rproc = ksproc->rproc;
  932. if (rproc->use_userspace_loader) {
  933. keystone_rproc_mem_del_attrs(ksproc);
  934. misc_deregister(&ksproc->misc);
  935. uio_unregister_device(&ksproc->uio);
  936. }
  937. rproc_del(ksproc->rproc);
  938. pm_runtime_put_sync(&pdev->dev);
  939. pm_runtime_disable(&pdev->dev);
  940. rproc_free(ksproc->rproc);
  941. of_reserved_mem_device_release(&pdev->dev);
  942. return 0;
  943. }
  944. static const struct of_device_id keystone_rproc_of_match[] = {
  945. { .compatible = "ti,k2hk-dsp", },
  946. { .compatible = "ti,k2l-dsp", },
  947. { .compatible = "ti,k2e-dsp", },
  948. { .compatible = "ti,k2g-dsp", },
  949. { /* sentinel */ },
  950. };
  951. MODULE_DEVICE_TABLE(of, keystone_rproc_of_match);
  952. static struct platform_driver keystone_rproc_driver = {
  953. .probe = keystone_rproc_probe,
  954. .remove = keystone_rproc_remove,
  955. .driver = {
  956. .name = "keystone-rproc",
  957. .of_match_table = keystone_rproc_of_match,
  958. },
  959. };
  960. static int __init keystone_rproc_init(void)
  961. {
  962. keystone_rproc_driver.driver.suppress_bind_attrs =
  963. !use_rproc_core_loader;
  964. return platform_driver_register(&keystone_rproc_driver);
  965. }
  966. module_init(keystone_rproc_init);
  967. static void __exit keystone_rproc_exit(void)
  968. {
  969. platform_driver_unregister(&keystone_rproc_driver);
  970. }
  971. module_exit(keystone_rproc_exit);
  972. MODULE_AUTHOR("Sam Nelson Siluvaimani");
  973. MODULE_AUTHOR("Suman Anna");
  974. MODULE_LICENSE("GPL v2");
  975. MODULE_DESCRIPTION("TI Keystone DSP Remoteproc driver");