mips_cdmm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * Bus driver for MIPS Common Device Memory Map (CDMM).
  3. *
  4. * Copyright (C) 2014-2015 Imagination Technologies Ltd.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/atomic.h>
  11. #include <linux/err.h>
  12. #include <linux/cpu.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/io.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/smp.h>
  20. #include <asm/cdmm.h>
  21. #include <asm/hazards.h>
  22. #include <asm/mipsregs.h>
  23. /* Access control and status register fields */
  24. #define CDMM_ACSR_DEVTYPE_SHIFT 24
  25. #define CDMM_ACSR_DEVTYPE (255ul << CDMM_ACSR_DEVTYPE_SHIFT)
  26. #define CDMM_ACSR_DEVSIZE_SHIFT 16
  27. #define CDMM_ACSR_DEVSIZE (31ul << CDMM_ACSR_DEVSIZE_SHIFT)
  28. #define CDMM_ACSR_DEVREV_SHIFT 12
  29. #define CDMM_ACSR_DEVREV (15ul << CDMM_ACSR_DEVREV_SHIFT)
  30. #define CDMM_ACSR_UW (1ul << 3)
  31. #define CDMM_ACSR_UR (1ul << 2)
  32. #define CDMM_ACSR_SW (1ul << 1)
  33. #define CDMM_ACSR_SR (1ul << 0)
  34. /* Each block of device registers is 64 bytes */
  35. #define CDMM_DRB_SIZE 64
  36. #define to_mips_cdmm_driver(d) container_of(d, struct mips_cdmm_driver, drv)
  37. /* Default physical base address */
  38. static phys_addr_t mips_cdmm_default_base;
  39. /* Bus operations */
  40. static const struct mips_cdmm_device_id *
  41. mips_cdmm_lookup(const struct mips_cdmm_device_id *table,
  42. struct mips_cdmm_device *dev)
  43. {
  44. int ret = 0;
  45. for (; table->type; ++table) {
  46. ret = (dev->type == table->type);
  47. if (ret)
  48. break;
  49. }
  50. return ret ? table : NULL;
  51. }
  52. static int mips_cdmm_match(struct device *dev, struct device_driver *drv)
  53. {
  54. struct mips_cdmm_device *cdev = to_mips_cdmm_device(dev);
  55. struct mips_cdmm_driver *cdrv = to_mips_cdmm_driver(drv);
  56. return mips_cdmm_lookup(cdrv->id_table, cdev) != NULL;
  57. }
  58. static int mips_cdmm_uevent(struct device *dev, struct kobj_uevent_env *env)
  59. {
  60. struct mips_cdmm_device *cdev = to_mips_cdmm_device(dev);
  61. int retval = 0;
  62. retval = add_uevent_var(env, "CDMM_CPU=%u", cdev->cpu);
  63. if (retval)
  64. return retval;
  65. retval = add_uevent_var(env, "CDMM_TYPE=0x%02x", cdev->type);
  66. if (retval)
  67. return retval;
  68. retval = add_uevent_var(env, "CDMM_REV=%u", cdev->rev);
  69. if (retval)
  70. return retval;
  71. retval = add_uevent_var(env, "MODALIAS=mipscdmm:t%02X", cdev->type);
  72. return retval;
  73. }
  74. /* Device attributes */
  75. #define CDMM_ATTR(name, fmt, arg...) \
  76. static ssize_t name##_show(struct device *_dev, \
  77. struct device_attribute *attr, char *buf) \
  78. { \
  79. struct mips_cdmm_device *dev = to_mips_cdmm_device(_dev); \
  80. return sprintf(buf, fmt, arg); \
  81. } \
  82. static DEVICE_ATTR_RO(name);
  83. CDMM_ATTR(cpu, "%u\n", dev->cpu);
  84. CDMM_ATTR(type, "0x%02x\n", dev->type);
  85. CDMM_ATTR(revision, "%u\n", dev->rev);
  86. CDMM_ATTR(modalias, "mipscdmm:t%02X\n", dev->type);
  87. CDMM_ATTR(resource, "\t%016llx\t%016llx\t%016lx\n",
  88. (unsigned long long)dev->res.start,
  89. (unsigned long long)dev->res.end,
  90. dev->res.flags);
  91. static struct attribute *mips_cdmm_dev_attrs[] = {
  92. &dev_attr_cpu.attr,
  93. &dev_attr_type.attr,
  94. &dev_attr_revision.attr,
  95. &dev_attr_modalias.attr,
  96. &dev_attr_resource.attr,
  97. NULL,
  98. };
  99. ATTRIBUTE_GROUPS(mips_cdmm_dev);
  100. struct bus_type mips_cdmm_bustype = {
  101. .name = "cdmm",
  102. .dev_groups = mips_cdmm_dev_groups,
  103. .match = mips_cdmm_match,
  104. .uevent = mips_cdmm_uevent,
  105. };
  106. EXPORT_SYMBOL_GPL(mips_cdmm_bustype);
  107. /*
  108. * Standard driver callback helpers.
  109. *
  110. * All the CDMM driver callbacks need to be executed on the appropriate CPU from
  111. * workqueues. For the standard driver callbacks we need a work function
  112. * (mips_cdmm_{void,int}_work()) to do the actual call from the right CPU, and a
  113. * wrapper function (generated with BUILD_PERCPU_HELPER) to arrange for the work
  114. * function to be called on that CPU.
  115. */
  116. /**
  117. * struct mips_cdmm_work_dev - Data for per-device call work.
  118. * @fn: CDMM driver callback function to call for the device.
  119. * @dev: CDMM device to pass to @fn.
  120. */
  121. struct mips_cdmm_work_dev {
  122. void *fn;
  123. struct mips_cdmm_device *dev;
  124. };
  125. /**
  126. * mips_cdmm_void_work() - Call a void returning CDMM driver callback.
  127. * @data: struct mips_cdmm_work_dev pointer.
  128. *
  129. * A work_on_cpu() callback function to call an arbitrary CDMM driver callback
  130. * function which doesn't return a value.
  131. */
  132. static long mips_cdmm_void_work(void *data)
  133. {
  134. struct mips_cdmm_work_dev *work = data;
  135. void (*fn)(struct mips_cdmm_device *) = work->fn;
  136. fn(work->dev);
  137. return 0;
  138. }
  139. /**
  140. * mips_cdmm_int_work() - Call an int returning CDMM driver callback.
  141. * @data: struct mips_cdmm_work_dev pointer.
  142. *
  143. * A work_on_cpu() callback function to call an arbitrary CDMM driver callback
  144. * function which returns an int.
  145. */
  146. static long mips_cdmm_int_work(void *data)
  147. {
  148. struct mips_cdmm_work_dev *work = data;
  149. int (*fn)(struct mips_cdmm_device *) = work->fn;
  150. return fn(work->dev);
  151. }
  152. #define _BUILD_RET_void
  153. #define _BUILD_RET_int return
  154. /**
  155. * BUILD_PERCPU_HELPER() - Helper to call a CDMM driver callback on right CPU.
  156. * @_ret: Return type (void or int).
  157. * @_name: Name of CDMM driver callback function.
  158. *
  159. * Generates a specific device callback function to call a CDMM driver callback
  160. * function on the appropriate CPU for the device, and if applicable return the
  161. * result.
  162. */
  163. #define BUILD_PERCPU_HELPER(_ret, _name) \
  164. static _ret mips_cdmm_##_name(struct device *dev) \
  165. { \
  166. struct mips_cdmm_device *cdev = to_mips_cdmm_device(dev); \
  167. struct mips_cdmm_driver *cdrv = to_mips_cdmm_driver(dev->driver); \
  168. struct mips_cdmm_work_dev work = { \
  169. .fn = cdrv->_name, \
  170. .dev = cdev, \
  171. }; \
  172. \
  173. _BUILD_RET_##_ret work_on_cpu(cdev->cpu, \
  174. mips_cdmm_##_ret##_work, &work); \
  175. }
  176. /* Driver callback functions */
  177. BUILD_PERCPU_HELPER(int, probe) /* int mips_cdmm_probe(struct device) */
  178. BUILD_PERCPU_HELPER(int, remove) /* int mips_cdmm_remove(struct device) */
  179. BUILD_PERCPU_HELPER(void, shutdown) /* void mips_cdmm_shutdown(struct device) */
  180. /* Driver registration */
  181. /**
  182. * mips_cdmm_driver_register() - Register a CDMM driver.
  183. * @drv: CDMM driver information.
  184. *
  185. * Register a CDMM driver with the CDMM subsystem. The driver will be informed
  186. * of matching devices which are discovered.
  187. *
  188. * Returns: 0 on success.
  189. */
  190. int mips_cdmm_driver_register(struct mips_cdmm_driver *drv)
  191. {
  192. drv->drv.bus = &mips_cdmm_bustype;
  193. if (drv->probe)
  194. drv->drv.probe = mips_cdmm_probe;
  195. if (drv->remove)
  196. drv->drv.remove = mips_cdmm_remove;
  197. if (drv->shutdown)
  198. drv->drv.shutdown = mips_cdmm_shutdown;
  199. return driver_register(&drv->drv);
  200. }
  201. EXPORT_SYMBOL_GPL(mips_cdmm_driver_register);
  202. /**
  203. * mips_cdmm_driver_unregister() - Unregister a CDMM driver.
  204. * @drv: CDMM driver information.
  205. *
  206. * Unregister a CDMM driver from the CDMM subsystem.
  207. */
  208. void mips_cdmm_driver_unregister(struct mips_cdmm_driver *drv)
  209. {
  210. driver_unregister(&drv->drv);
  211. }
  212. EXPORT_SYMBOL_GPL(mips_cdmm_driver_unregister);
  213. /* CDMM initialisation and bus discovery */
  214. /**
  215. * struct mips_cdmm_bus - Info about CDMM bus.
  216. * @phys: Physical address at which it is mapped.
  217. * @regs: Virtual address where registers can be accessed.
  218. * @drbs: Total number of DRBs.
  219. * @drbs_reserved: Number of DRBs reserved.
  220. * @discovered: Whether the devices on the bus have been discovered yet.
  221. * @offline: Whether the CDMM bus is going offline (or very early
  222. * coming back online), in which case it should be
  223. * reconfigured each time.
  224. */
  225. struct mips_cdmm_bus {
  226. phys_addr_t phys;
  227. void __iomem *regs;
  228. unsigned int drbs;
  229. unsigned int drbs_reserved;
  230. bool discovered;
  231. bool offline;
  232. };
  233. static struct mips_cdmm_bus mips_cdmm_boot_bus;
  234. static DEFINE_PER_CPU(struct mips_cdmm_bus *, mips_cdmm_buses);
  235. static atomic_t mips_cdmm_next_id = ATOMIC_INIT(-1);
  236. /**
  237. * mips_cdmm_get_bus() - Get the per-CPU CDMM bus information.
  238. *
  239. * Get information about the per-CPU CDMM bus, if the bus is present.
  240. *
  241. * The caller must prevent migration to another CPU, either by disabling
  242. * pre-emption or by running from a pinned kernel thread.
  243. *
  244. * Returns: Pointer to CDMM bus information for the current CPU.
  245. * May return ERR_PTR(-errno) in case of error, so check with
  246. * IS_ERR().
  247. */
  248. static struct mips_cdmm_bus *mips_cdmm_get_bus(void)
  249. {
  250. struct mips_cdmm_bus *bus, **bus_p;
  251. unsigned long flags;
  252. unsigned int cpu;
  253. if (!cpu_has_cdmm)
  254. return ERR_PTR(-ENODEV);
  255. cpu = smp_processor_id();
  256. /* Avoid early use of per-cpu primitives before initialised */
  257. if (cpu == 0)
  258. return &mips_cdmm_boot_bus;
  259. /* Get bus pointer */
  260. bus_p = per_cpu_ptr(&mips_cdmm_buses, cpu);
  261. local_irq_save(flags);
  262. bus = *bus_p;
  263. /* Attempt allocation if NULL */
  264. if (unlikely(!bus)) {
  265. bus = kzalloc(sizeof(*bus), GFP_ATOMIC);
  266. if (unlikely(!bus))
  267. bus = ERR_PTR(-ENOMEM);
  268. else
  269. *bus_p = bus;
  270. }
  271. local_irq_restore(flags);
  272. return bus;
  273. }
  274. /**
  275. * mips_cdmm_cur_base() - Find current physical base address of CDMM region.
  276. *
  277. * Returns: Physical base address of CDMM region according to cdmmbase CP0
  278. * register, or 0 if the CDMM region is disabled.
  279. */
  280. static phys_addr_t mips_cdmm_cur_base(void)
  281. {
  282. unsigned long cdmmbase = read_c0_cdmmbase();
  283. if (!(cdmmbase & MIPS_CDMMBASE_EN))
  284. return 0;
  285. return (cdmmbase >> MIPS_CDMMBASE_ADDR_SHIFT)
  286. << MIPS_CDMMBASE_ADDR_START;
  287. }
  288. /**
  289. * mips_cdmm_phys_base() - Choose a physical base address for CDMM region.
  290. *
  291. * Picking a suitable physical address at which to map the CDMM region is
  292. * platform specific, so this weak function can be overridden by platform
  293. * code to pick a suitable value if none is configured by the bootloader.
  294. * By default this method tries to find a CDMM-specific node in the system
  295. * dtb. Note that this won't work for early serial console.
  296. */
  297. phys_addr_t __weak mips_cdmm_phys_base(void)
  298. {
  299. struct device_node *np;
  300. struct resource res;
  301. int err;
  302. np = of_find_compatible_node(NULL, NULL, "mti,mips-cdmm");
  303. if (np) {
  304. err = of_address_to_resource(np, 0, &res);
  305. of_node_put(np);
  306. if (!err)
  307. return res.start;
  308. }
  309. return 0;
  310. }
  311. /**
  312. * mips_cdmm_setup() - Ensure the CDMM bus is initialised and usable.
  313. * @bus: Pointer to bus information for current CPU.
  314. * IS_ERR(bus) is checked, so no need for caller to check.
  315. *
  316. * The caller must prevent migration to another CPU, either by disabling
  317. * pre-emption or by running from a pinned kernel thread.
  318. *
  319. * Returns 0 on success, -errno on failure.
  320. */
  321. static int mips_cdmm_setup(struct mips_cdmm_bus *bus)
  322. {
  323. unsigned long cdmmbase, flags;
  324. int ret = 0;
  325. if (IS_ERR(bus))
  326. return PTR_ERR(bus);
  327. local_irq_save(flags);
  328. /* Don't set up bus a second time unless marked offline */
  329. if (bus->offline) {
  330. /* If CDMM region is still set up, nothing to do */
  331. if (bus->phys == mips_cdmm_cur_base())
  332. goto out;
  333. /*
  334. * The CDMM region isn't set up as expected, so it needs
  335. * reconfiguring, but then we can stop checking it.
  336. */
  337. bus->offline = false;
  338. } else if (bus->phys > 1) {
  339. goto out;
  340. }
  341. /* If the CDMM region is already configured, inherit that setup */
  342. if (!bus->phys)
  343. bus->phys = mips_cdmm_cur_base();
  344. /* Otherwise, ask platform code for suggestions */
  345. if (!bus->phys)
  346. bus->phys = mips_cdmm_phys_base();
  347. /* Otherwise, copy what other CPUs have done */
  348. if (!bus->phys)
  349. bus->phys = mips_cdmm_default_base;
  350. /* Otherwise, complain once */
  351. if (!bus->phys) {
  352. bus->phys = 1;
  353. /*
  354. * If you hit this, either your bootloader needs to set up the
  355. * CDMM on the boot CPU, or else you need to implement
  356. * mips_cdmm_phys_base() for your platform (see asm/cdmm.h).
  357. */
  358. pr_err("cdmm%u: Failed to choose a physical base\n",
  359. smp_processor_id());
  360. }
  361. /* Already complained? */
  362. if (bus->phys == 1) {
  363. ret = -ENOMEM;
  364. goto out;
  365. }
  366. /* Record our success for other CPUs to copy */
  367. mips_cdmm_default_base = bus->phys;
  368. pr_debug("cdmm%u: Enabling CDMM region at %pa\n",
  369. smp_processor_id(), &bus->phys);
  370. /* Enable CDMM */
  371. cdmmbase = read_c0_cdmmbase();
  372. cdmmbase &= (1ul << MIPS_CDMMBASE_ADDR_SHIFT) - 1;
  373. cdmmbase |= (bus->phys >> MIPS_CDMMBASE_ADDR_START)
  374. << MIPS_CDMMBASE_ADDR_SHIFT;
  375. cdmmbase |= MIPS_CDMMBASE_EN;
  376. write_c0_cdmmbase(cdmmbase);
  377. tlbw_use_hazard();
  378. bus->regs = (void __iomem *)CKSEG1ADDR(bus->phys);
  379. bus->drbs = 1 + ((cdmmbase & MIPS_CDMMBASE_SIZE) >>
  380. MIPS_CDMMBASE_SIZE_SHIFT);
  381. bus->drbs_reserved = !!(cdmmbase & MIPS_CDMMBASE_CI);
  382. out:
  383. local_irq_restore(flags);
  384. return ret;
  385. }
  386. /**
  387. * mips_cdmm_early_probe() - Minimally probe for a specific device on CDMM.
  388. * @dev_type: CDMM type code to look for.
  389. *
  390. * Minimally configure the in-CPU Common Device Memory Map (CDMM) and look for a
  391. * specific device. This can be used to find a device very early in boot for
  392. * example to configure an early FDC console device.
  393. *
  394. * The caller must prevent migration to another CPU, either by disabling
  395. * pre-emption or by running from a pinned kernel thread.
  396. *
  397. * Returns: MMIO pointer to device memory. The caller can read the ACSR
  398. * register to find more information about the device (such as the
  399. * version number or the number of blocks).
  400. * May return IOMEM_ERR_PTR(-errno) in case of error, so check with
  401. * IS_ERR().
  402. */
  403. void __iomem *mips_cdmm_early_probe(unsigned int dev_type)
  404. {
  405. struct mips_cdmm_bus *bus;
  406. void __iomem *cdmm;
  407. u32 acsr;
  408. unsigned int drb, type, size;
  409. int err;
  410. if (WARN_ON(!dev_type))
  411. return IOMEM_ERR_PTR(-ENODEV);
  412. bus = mips_cdmm_get_bus();
  413. err = mips_cdmm_setup(bus);
  414. if (err)
  415. return IOMEM_ERR_PTR(err);
  416. /* Skip the first block if it's reserved for more registers */
  417. drb = bus->drbs_reserved;
  418. cdmm = bus->regs;
  419. /* Look for a specific device type */
  420. for (; drb < bus->drbs; drb += size + 1) {
  421. acsr = __raw_readl(cdmm + drb * CDMM_DRB_SIZE);
  422. type = (acsr & CDMM_ACSR_DEVTYPE) >> CDMM_ACSR_DEVTYPE_SHIFT;
  423. if (type == dev_type)
  424. return cdmm + drb * CDMM_DRB_SIZE;
  425. size = (acsr & CDMM_ACSR_DEVSIZE) >> CDMM_ACSR_DEVSIZE_SHIFT;
  426. }
  427. return IOMEM_ERR_PTR(-ENODEV);
  428. }
  429. EXPORT_SYMBOL_GPL(mips_cdmm_early_probe);
  430. /**
  431. * mips_cdmm_release() - Release a removed CDMM device.
  432. * @dev: Device object
  433. *
  434. * Clean up the struct mips_cdmm_device for an unused CDMM device. This is
  435. * called automatically by the driver core when a device is removed.
  436. */
  437. static void mips_cdmm_release(struct device *dev)
  438. {
  439. struct mips_cdmm_device *cdev = to_mips_cdmm_device(dev);
  440. kfree(cdev);
  441. }
  442. /**
  443. * mips_cdmm_bus_discover() - Discover the devices on the CDMM bus.
  444. * @bus: CDMM bus information, must already be set up.
  445. */
  446. static void mips_cdmm_bus_discover(struct mips_cdmm_bus *bus)
  447. {
  448. void __iomem *cdmm;
  449. u32 acsr;
  450. unsigned int drb, type, size, rev;
  451. struct mips_cdmm_device *dev;
  452. unsigned int cpu = smp_processor_id();
  453. int ret = 0;
  454. int id = 0;
  455. /* Skip the first block if it's reserved for more registers */
  456. drb = bus->drbs_reserved;
  457. cdmm = bus->regs;
  458. /* Discover devices */
  459. bus->discovered = true;
  460. pr_info("cdmm%u discovery (%u blocks)\n", cpu, bus->drbs);
  461. for (; drb < bus->drbs; drb += size + 1) {
  462. acsr = __raw_readl(cdmm + drb * CDMM_DRB_SIZE);
  463. type = (acsr & CDMM_ACSR_DEVTYPE) >> CDMM_ACSR_DEVTYPE_SHIFT;
  464. size = (acsr & CDMM_ACSR_DEVSIZE) >> CDMM_ACSR_DEVSIZE_SHIFT;
  465. rev = (acsr & CDMM_ACSR_DEVREV) >> CDMM_ACSR_DEVREV_SHIFT;
  466. if (!type)
  467. continue;
  468. pr_info("cdmm%u-%u: @%u (%#x..%#x), type 0x%02x, rev %u\n",
  469. cpu, id, drb, drb * CDMM_DRB_SIZE,
  470. (drb + size + 1) * CDMM_DRB_SIZE - 1,
  471. type, rev);
  472. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  473. if (!dev)
  474. break;
  475. dev->cpu = cpu;
  476. dev->res.start = bus->phys + drb * CDMM_DRB_SIZE;
  477. dev->res.end = bus->phys +
  478. (drb + size + 1) * CDMM_DRB_SIZE - 1;
  479. dev->res.flags = IORESOURCE_MEM;
  480. dev->type = type;
  481. dev->rev = rev;
  482. dev->dev.parent = get_cpu_device(cpu);
  483. dev->dev.bus = &mips_cdmm_bustype;
  484. dev->dev.id = atomic_inc_return(&mips_cdmm_next_id);
  485. dev->dev.release = mips_cdmm_release;
  486. dev_set_name(&dev->dev, "cdmm%u-%u", cpu, id);
  487. ++id;
  488. ret = device_register(&dev->dev);
  489. if (ret)
  490. put_device(&dev->dev);
  491. }
  492. }
  493. /*
  494. * CPU hotplug and initialisation
  495. *
  496. * All the CDMM driver callbacks need to be executed on the appropriate CPU from
  497. * workqueues. For the CPU callbacks, they need to be called for all devices on
  498. * that CPU, so the work function calls bus_for_each_dev, using a helper
  499. * (generated with BUILD_PERDEV_HELPER) to call the driver callback if the
  500. * device's CPU matches.
  501. */
  502. /**
  503. * BUILD_PERDEV_HELPER() - Helper to call a CDMM driver callback if CPU matches.
  504. * @_name: Name of CDMM driver callback function.
  505. *
  506. * Generates a bus_for_each_dev callback function to call a specific CDMM driver
  507. * callback function for the device if the device's CPU matches that pointed to
  508. * by the data argument.
  509. *
  510. * This is used for informing drivers for all devices on a given CPU of some
  511. * event (such as the CPU going online/offline).
  512. *
  513. * It is expected to already be called from the appropriate CPU.
  514. */
  515. #define BUILD_PERDEV_HELPER(_name) \
  516. static int mips_cdmm_##_name##_helper(struct device *dev, void *data) \
  517. { \
  518. struct mips_cdmm_device *cdev = to_mips_cdmm_device(dev); \
  519. struct mips_cdmm_driver *cdrv; \
  520. unsigned int cpu = *(unsigned int *)data; \
  521. \
  522. if (cdev->cpu != cpu || !dev->driver) \
  523. return 0; \
  524. \
  525. cdrv = to_mips_cdmm_driver(dev->driver); \
  526. if (!cdrv->_name) \
  527. return 0; \
  528. return cdrv->_name(cdev); \
  529. }
  530. /* bus_for_each_dev callback helper functions */
  531. BUILD_PERDEV_HELPER(cpu_down) /* int mips_cdmm_cpu_down_helper(...) */
  532. BUILD_PERDEV_HELPER(cpu_up) /* int mips_cdmm_cpu_up_helper(...) */
  533. /**
  534. * mips_cdmm_cpu_down_prep() - Callback for CPUHP DOWN_PREP:
  535. * Tear down the CDMM bus.
  536. * @cpu: unsigned int CPU number.
  537. *
  538. * This function is executed on the hotplugged CPU and calls the CDMM
  539. * driver cpu_down callback for all devices on that CPU.
  540. */
  541. static int mips_cdmm_cpu_down_prep(unsigned int cpu)
  542. {
  543. struct mips_cdmm_bus *bus;
  544. long ret;
  545. /* Inform all the devices on the bus */
  546. ret = bus_for_each_dev(&mips_cdmm_bustype, NULL, &cpu,
  547. mips_cdmm_cpu_down_helper);
  548. /*
  549. * While bus is offline, each use of it should reconfigure it just in
  550. * case it is first use when coming back online again.
  551. */
  552. bus = mips_cdmm_get_bus();
  553. if (!IS_ERR(bus))
  554. bus->offline = true;
  555. return ret;
  556. }
  557. /**
  558. * mips_cdmm_cpu_online() - Callback for CPUHP ONLINE: Bring up the CDMM bus.
  559. * @cpu: unsigned int CPU number.
  560. *
  561. * This work_on_cpu callback function is executed on a given CPU to discover
  562. * CDMM devices on that CPU, or to call the CDMM driver cpu_up callback for all
  563. * devices already discovered on that CPU.
  564. *
  565. * It is used as work_on_cpu callback function during
  566. * initialisation. When CPUs are brought online the function is
  567. * invoked directly on the hotplugged CPU.
  568. */
  569. static int mips_cdmm_cpu_online(unsigned int cpu)
  570. {
  571. struct mips_cdmm_bus *bus;
  572. long ret;
  573. bus = mips_cdmm_get_bus();
  574. ret = mips_cdmm_setup(bus);
  575. if (ret)
  576. return ret;
  577. /* Bus now set up, so we can drop the offline flag if still set */
  578. bus->offline = false;
  579. if (!bus->discovered)
  580. mips_cdmm_bus_discover(bus);
  581. else
  582. /* Inform all the devices on the bus */
  583. ret = bus_for_each_dev(&mips_cdmm_bustype, NULL, &cpu,
  584. mips_cdmm_cpu_up_helper);
  585. return ret;
  586. }
  587. /**
  588. * mips_cdmm_init() - Initialise CDMM bus.
  589. *
  590. * Initialise CDMM bus, discover CDMM devices for online CPUs, and arrange for
  591. * hotplug notifications so the CDMM drivers can be kept up to date.
  592. */
  593. static int __init mips_cdmm_init(void)
  594. {
  595. int ret;
  596. /* Register the bus */
  597. ret = bus_register(&mips_cdmm_bustype);
  598. if (ret)
  599. return ret;
  600. /* We want to be notified about new CPUs */
  601. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "bus/cdmm:online",
  602. mips_cdmm_cpu_online, mips_cdmm_cpu_down_prep);
  603. if (ret < 0)
  604. pr_warn("cdmm: Failed to register CPU notifier\n");
  605. return ret;
  606. }
  607. subsys_initcall(mips_cdmm_init);