mips_cdmm.c 19 KB

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