mips_cdmm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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_setup() - Ensure the CDMM bus is initialised and usable.
  288. * @bus: Pointer to bus information for current CPU.
  289. * IS_ERR(bus) is checked, so no need for caller to check.
  290. *
  291. * The caller must prevent migration to another CPU, either by disabling
  292. * pre-emption or by running from a pinned kernel thread.
  293. *
  294. * Returns 0 on success, -errno on failure.
  295. */
  296. static int mips_cdmm_setup(struct mips_cdmm_bus *bus)
  297. {
  298. unsigned long cdmmbase, flags;
  299. int ret = 0;
  300. if (IS_ERR(bus))
  301. return PTR_ERR(bus);
  302. local_irq_save(flags);
  303. /* Don't set up bus a second time unless marked offline */
  304. if (bus->offline) {
  305. /* If CDMM region is still set up, nothing to do */
  306. if (bus->phys == mips_cdmm_cur_base())
  307. goto out;
  308. /*
  309. * The CDMM region isn't set up as expected, so it needs
  310. * reconfiguring, but then we can stop checking it.
  311. */
  312. bus->offline = false;
  313. } else if (bus->phys > 1) {
  314. goto out;
  315. }
  316. /* If the CDMM region is already configured, inherit that setup */
  317. if (!bus->phys)
  318. bus->phys = mips_cdmm_cur_base();
  319. /* Otherwise, ask platform code for suggestions */
  320. if (!bus->phys && mips_cdmm_phys_base)
  321. bus->phys = mips_cdmm_phys_base();
  322. /* Otherwise, copy what other CPUs have done */
  323. if (!bus->phys)
  324. bus->phys = mips_cdmm_default_base;
  325. /* Otherwise, complain once */
  326. if (!bus->phys) {
  327. bus->phys = 1;
  328. /*
  329. * If you hit this, either your bootloader needs to set up the
  330. * CDMM on the boot CPU, or else you need to implement
  331. * mips_cdmm_phys_base() for your platform (see asm/cdmm.h).
  332. */
  333. pr_err("cdmm%u: Failed to choose a physical base\n",
  334. smp_processor_id());
  335. }
  336. /* Already complained? */
  337. if (bus->phys == 1) {
  338. ret = -ENOMEM;
  339. goto out;
  340. }
  341. /* Record our success for other CPUs to copy */
  342. mips_cdmm_default_base = bus->phys;
  343. pr_debug("cdmm%u: Enabling CDMM region at %pa\n",
  344. smp_processor_id(), &bus->phys);
  345. /* Enable CDMM */
  346. cdmmbase = read_c0_cdmmbase();
  347. cdmmbase &= (1ul << MIPS_CDMMBASE_ADDR_SHIFT) - 1;
  348. cdmmbase |= (bus->phys >> MIPS_CDMMBASE_ADDR_START)
  349. << MIPS_CDMMBASE_ADDR_SHIFT;
  350. cdmmbase |= MIPS_CDMMBASE_EN;
  351. write_c0_cdmmbase(cdmmbase);
  352. tlbw_use_hazard();
  353. bus->regs = (void __iomem *)CKSEG1ADDR(bus->phys);
  354. bus->drbs = 1 + ((cdmmbase & MIPS_CDMMBASE_SIZE) >>
  355. MIPS_CDMMBASE_SIZE_SHIFT);
  356. bus->drbs_reserved = !!(cdmmbase & MIPS_CDMMBASE_CI);
  357. out:
  358. local_irq_restore(flags);
  359. return ret;
  360. }
  361. /**
  362. * mips_cdmm_early_probe() - Minimally probe for a specific device on CDMM.
  363. * @dev_type: CDMM type code to look for.
  364. *
  365. * Minimally configure the in-CPU Common Device Memory Map (CDMM) and look for a
  366. * specific device. This can be used to find a device very early in boot for
  367. * example to configure an early FDC console device.
  368. *
  369. * The caller must prevent migration to another CPU, either by disabling
  370. * pre-emption or by running from a pinned kernel thread.
  371. *
  372. * Returns: MMIO pointer to device memory. The caller can read the ACSR
  373. * register to find more information about the device (such as the
  374. * version number or the number of blocks).
  375. * May return IOMEM_ERR_PTR(-errno) in case of error, so check with
  376. * IS_ERR().
  377. */
  378. void __iomem *mips_cdmm_early_probe(unsigned int dev_type)
  379. {
  380. struct mips_cdmm_bus *bus;
  381. void __iomem *cdmm;
  382. u32 acsr;
  383. unsigned int drb, type, size;
  384. int err;
  385. if (WARN_ON(!dev_type))
  386. return IOMEM_ERR_PTR(-ENODEV);
  387. bus = mips_cdmm_get_bus();
  388. err = mips_cdmm_setup(bus);
  389. if (err)
  390. return IOMEM_ERR_PTR(err);
  391. /* Skip the first block if it's reserved for more registers */
  392. drb = bus->drbs_reserved;
  393. cdmm = bus->regs;
  394. /* Look for a specific device type */
  395. for (; drb < bus->drbs; drb += size + 1) {
  396. acsr = __raw_readl(cdmm + drb * CDMM_DRB_SIZE);
  397. type = (acsr & CDMM_ACSR_DEVTYPE) >> CDMM_ACSR_DEVTYPE_SHIFT;
  398. if (type == dev_type)
  399. return cdmm + drb * CDMM_DRB_SIZE;
  400. size = (acsr & CDMM_ACSR_DEVSIZE) >> CDMM_ACSR_DEVSIZE_SHIFT;
  401. }
  402. return IOMEM_ERR_PTR(-ENODEV);
  403. }
  404. EXPORT_SYMBOL_GPL(mips_cdmm_early_probe);
  405. /**
  406. * mips_cdmm_release() - Release a removed CDMM device.
  407. * @dev: Device object
  408. *
  409. * Clean up the struct mips_cdmm_device for an unused CDMM device. This is
  410. * called automatically by the driver core when a device is removed.
  411. */
  412. static void mips_cdmm_release(struct device *dev)
  413. {
  414. struct mips_cdmm_device *cdev = to_mips_cdmm_device(dev);
  415. kfree(cdev);
  416. }
  417. /**
  418. * mips_cdmm_bus_discover() - Discover the devices on the CDMM bus.
  419. * @bus: CDMM bus information, must already be set up.
  420. */
  421. static void mips_cdmm_bus_discover(struct mips_cdmm_bus *bus)
  422. {
  423. void __iomem *cdmm;
  424. u32 acsr;
  425. unsigned int drb, type, size, rev;
  426. struct mips_cdmm_device *dev;
  427. unsigned int cpu = smp_processor_id();
  428. int ret = 0;
  429. int id = 0;
  430. /* Skip the first block if it's reserved for more registers */
  431. drb = bus->drbs_reserved;
  432. cdmm = bus->regs;
  433. /* Discover devices */
  434. bus->discovered = true;
  435. pr_info("cdmm%u discovery (%u blocks)\n", cpu, bus->drbs);
  436. for (; drb < bus->drbs; drb += size + 1) {
  437. acsr = __raw_readl(cdmm + drb * CDMM_DRB_SIZE);
  438. type = (acsr & CDMM_ACSR_DEVTYPE) >> CDMM_ACSR_DEVTYPE_SHIFT;
  439. size = (acsr & CDMM_ACSR_DEVSIZE) >> CDMM_ACSR_DEVSIZE_SHIFT;
  440. rev = (acsr & CDMM_ACSR_DEVREV) >> CDMM_ACSR_DEVREV_SHIFT;
  441. if (!type)
  442. continue;
  443. pr_info("cdmm%u-%u: @%u (%#x..%#x), type 0x%02x, rev %u\n",
  444. cpu, id, drb, drb * CDMM_DRB_SIZE,
  445. (drb + size + 1) * CDMM_DRB_SIZE - 1,
  446. type, rev);
  447. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  448. if (!dev)
  449. break;
  450. dev->cpu = cpu;
  451. dev->res.start = bus->phys + drb * CDMM_DRB_SIZE;
  452. dev->res.end = bus->phys +
  453. (drb + size + 1) * CDMM_DRB_SIZE - 1;
  454. dev->res.flags = IORESOURCE_MEM;
  455. dev->type = type;
  456. dev->rev = rev;
  457. dev->dev.parent = get_cpu_device(cpu);
  458. dev->dev.bus = &mips_cdmm_bustype;
  459. dev->dev.id = atomic_inc_return(&mips_cdmm_next_id);
  460. dev->dev.release = mips_cdmm_release;
  461. dev_set_name(&dev->dev, "cdmm%u-%u", cpu, id);
  462. ++id;
  463. ret = device_register(&dev->dev);
  464. if (ret) {
  465. put_device(&dev->dev);
  466. kfree(dev);
  467. }
  468. }
  469. }
  470. /*
  471. * CPU hotplug and initialisation
  472. *
  473. * All the CDMM driver callbacks need to be executed on the appropriate CPU from
  474. * workqueues. For the CPU callbacks, they need to be called for all devices on
  475. * that CPU, so the work function calls bus_for_each_dev, using a helper
  476. * (generated with BUILD_PERDEV_HELPER) to call the driver callback if the
  477. * device's CPU matches.
  478. */
  479. /**
  480. * BUILD_PERDEV_HELPER() - Helper to call a CDMM driver callback if CPU matches.
  481. * @_name: Name of CDMM driver callback function.
  482. *
  483. * Generates a bus_for_each_dev callback function to call a specific CDMM driver
  484. * callback function for the device if the device's CPU matches that pointed to
  485. * by the data argument.
  486. *
  487. * This is used for informing drivers for all devices on a given CPU of some
  488. * event (such as the CPU going online/offline).
  489. *
  490. * It is expected to already be called from the appropriate CPU.
  491. */
  492. #define BUILD_PERDEV_HELPER(_name) \
  493. static int mips_cdmm_##_name##_helper(struct device *dev, void *data) \
  494. { \
  495. struct mips_cdmm_device *cdev = to_mips_cdmm_device(dev); \
  496. struct mips_cdmm_driver *cdrv; \
  497. unsigned int cpu = *(unsigned int *)data; \
  498. \
  499. if (cdev->cpu != cpu || !dev->driver) \
  500. return 0; \
  501. \
  502. cdrv = to_mips_cdmm_driver(dev->driver); \
  503. if (!cdrv->_name) \
  504. return 0; \
  505. return cdrv->_name(cdev); \
  506. }
  507. /* bus_for_each_dev callback helper functions */
  508. BUILD_PERDEV_HELPER(cpu_down) /* int mips_cdmm_cpu_down_helper(...) */
  509. BUILD_PERDEV_HELPER(cpu_up) /* int mips_cdmm_cpu_up_helper(...) */
  510. /**
  511. * mips_cdmm_bus_down() - Tear down the CDMM bus.
  512. * @data: Pointer to unsigned int CPU number.
  513. *
  514. * This work_on_cpu callback function is executed on a given CPU to call the
  515. * CDMM driver cpu_down callback for all devices on that CPU.
  516. */
  517. static long mips_cdmm_bus_down(void *data)
  518. {
  519. struct mips_cdmm_bus *bus;
  520. long ret;
  521. /* Inform all the devices on the bus */
  522. ret = bus_for_each_dev(&mips_cdmm_bustype, NULL, data,
  523. mips_cdmm_cpu_down_helper);
  524. /*
  525. * While bus is offline, each use of it should reconfigure it just in
  526. * case it is first use when coming back online again.
  527. */
  528. bus = mips_cdmm_get_bus();
  529. if (!IS_ERR(bus))
  530. bus->offline = true;
  531. return ret;
  532. }
  533. /**
  534. * mips_cdmm_bus_up() - Bring up the CDMM bus.
  535. * @data: Pointer to unsigned int CPU number.
  536. *
  537. * This work_on_cpu callback function is executed on a given CPU to discover
  538. * CDMM devices on that CPU, or to call the CDMM driver cpu_up callback for all
  539. * devices already discovered on that CPU.
  540. *
  541. * It is used during initialisation and when CPUs are brought online.
  542. */
  543. static long mips_cdmm_bus_up(void *data)
  544. {
  545. struct mips_cdmm_bus *bus;
  546. long ret;
  547. bus = mips_cdmm_get_bus();
  548. ret = mips_cdmm_setup(bus);
  549. if (ret)
  550. return ret;
  551. /* Bus now set up, so we can drop the offline flag if still set */
  552. bus->offline = false;
  553. if (!bus->discovered)
  554. mips_cdmm_bus_discover(bus);
  555. else
  556. /* Inform all the devices on the bus */
  557. ret = bus_for_each_dev(&mips_cdmm_bustype, NULL, data,
  558. mips_cdmm_cpu_up_helper);
  559. return ret;
  560. }
  561. /**
  562. * mips_cdmm_cpu_notify() - Take action when a CPU is going online or offline.
  563. * @nb: CPU notifier block .
  564. * @action: Event that has taken place (CPU_*).
  565. * @data: CPU number.
  566. *
  567. * This notifier is used to keep the CDMM buses updated as CPUs are offlined and
  568. * onlined. When CPUs go offline or come back online, so does their CDMM bus, so
  569. * devices must be informed. Also when CPUs come online for the first time the
  570. * devices on the CDMM bus need discovering.
  571. *
  572. * Returns: NOTIFY_OK if event was used.
  573. * NOTIFY_DONE if we didn't care.
  574. */
  575. static int mips_cdmm_cpu_notify(struct notifier_block *nb,
  576. unsigned long action, void *data)
  577. {
  578. unsigned int cpu = (unsigned int)data;
  579. switch (action & ~CPU_TASKS_FROZEN) {
  580. case CPU_ONLINE:
  581. case CPU_DOWN_FAILED:
  582. work_on_cpu(cpu, mips_cdmm_bus_up, &cpu);
  583. break;
  584. case CPU_DOWN_PREPARE:
  585. work_on_cpu(cpu, mips_cdmm_bus_down, &cpu);
  586. break;
  587. default:
  588. return NOTIFY_DONE;
  589. }
  590. return NOTIFY_OK;
  591. }
  592. static struct notifier_block mips_cdmm_cpu_nb = {
  593. .notifier_call = mips_cdmm_cpu_notify,
  594. };
  595. /**
  596. * mips_cdmm_init() - Initialise CDMM bus.
  597. *
  598. * Initialise CDMM bus, discover CDMM devices for online CPUs, and arrange for
  599. * hotplug notifications so the CDMM drivers can be kept up to date.
  600. */
  601. static int __init mips_cdmm_init(void)
  602. {
  603. unsigned int cpu;
  604. int ret;
  605. /* Register the bus */
  606. ret = bus_register(&mips_cdmm_bustype);
  607. if (ret)
  608. return ret;
  609. /* We want to be notified about new CPUs */
  610. ret = register_cpu_notifier(&mips_cdmm_cpu_nb);
  611. if (ret) {
  612. pr_warn("cdmm: Failed to register CPU notifier\n");
  613. goto out;
  614. }
  615. /* Discover devices on CDMM of online CPUs */
  616. for_each_online_cpu(cpu)
  617. work_on_cpu(cpu, mips_cdmm_bus_up, &cpu);
  618. return 0;
  619. out:
  620. bus_unregister(&mips_cdmm_bustype);
  621. return ret;
  622. }
  623. subsys_initcall(mips_cdmm_init);