mpam_devices.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2025 Arm Ltd.
  3. #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
  4. #include <linux/acpi.h>
  5. #include <linux/atomic.h>
  6. #include <linux/arm_mpam.h>
  7. #include <linux/bitfield.h>
  8. #include <linux/bitmap.h>
  9. #include <linux/cacheinfo.h>
  10. #include <linux/cpu.h>
  11. #include <linux/cpumask.h>
  12. #include <linux/device.h>
  13. #include <linux/errno.h>
  14. #include <linux/gfp.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/irqdesc.h>
  18. #include <linux/list.h>
  19. #include <linux/lockdep.h>
  20. #include <linux/mutex.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/printk.h>
  23. #include <linux/srcu.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/types.h>
  26. #include <linux/workqueue.h>
  27. #include "mpam_internal.h"
  28. DEFINE_STATIC_KEY_FALSE(mpam_enabled); /* This moves to arch code */
  29. /*
  30. * mpam_list_lock protects the SRCU lists when writing. Once the
  31. * mpam_enabled key is enabled these lists are read-only,
  32. * unless the error interrupt disables the driver.
  33. */
  34. static DEFINE_MUTEX(mpam_list_lock);
  35. static LIST_HEAD(mpam_all_msc);
  36. struct srcu_struct mpam_srcu;
  37. /*
  38. * Number of MSCs that have been probed. Once all MSCs have been probed MPAM
  39. * can be enabled.
  40. */
  41. static atomic_t mpam_num_msc;
  42. static int mpam_cpuhp_state;
  43. static DEFINE_MUTEX(mpam_cpuhp_state_lock);
  44. /*
  45. * The smallest common values for any CPU or MSC in the system.
  46. * Generating traffic outside this range will result in screaming interrupts.
  47. */
  48. u16 mpam_partid_max;
  49. u8 mpam_pmg_max;
  50. static bool partid_max_init, partid_max_published;
  51. static DEFINE_SPINLOCK(partid_max_lock);
  52. /*
  53. * mpam is enabled once all devices have been probed from CPU online callbacks,
  54. * scheduled via this work_struct. If access to an MSC depends on a CPU that
  55. * was not brought online at boot, this can happen surprisingly late.
  56. */
  57. static DECLARE_WORK(mpam_enable_work, &mpam_enable);
  58. /*
  59. * All mpam error interrupts indicate a software bug. On receipt, disable the
  60. * driver.
  61. */
  62. static DECLARE_WORK(mpam_broken_work, &mpam_disable);
  63. /* When mpam is disabled, the printed reason to aid debugging */
  64. static char *mpam_disable_reason;
  65. /*
  66. * An MSC is a physical container for controls and monitors, each identified by
  67. * their RIS index. These share a base-address, interrupts and some MMIO
  68. * registers. A vMSC is a virtual container for RIS in an MSC that control or
  69. * monitor the same thing. Members of a vMSC are all RIS in the same MSC, but
  70. * not all RIS in an MSC share a vMSC.
  71. *
  72. * Components are a group of vMSC that control or monitor the same thing but
  73. * are from different MSC, so have different base-address, interrupts etc.
  74. * Classes are the set components of the same type.
  75. *
  76. * The features of a vMSC is the union of the RIS it contains.
  77. * The features of a Class and Component are the common subset of the vMSC
  78. * they contain.
  79. *
  80. * e.g. The system cache may have bandwidth controls on multiple interfaces,
  81. * for regulating traffic from devices independently of traffic from CPUs.
  82. * If these are two RIS in one MSC, they will be treated as controlling
  83. * different things, and will not share a vMSC/component/class.
  84. *
  85. * e.g. The L2 may have one MSC and two RIS, one for cache-controls another
  86. * for bandwidth. These two RIS are members of the same vMSC.
  87. *
  88. * e.g. The set of RIS that make up the L2 are grouped as a component. These
  89. * are sometimes termed slices. They should be configured the same, as if there
  90. * were only one.
  91. *
  92. * e.g. The SoC probably has more than one L2, each attached to a distinct set
  93. * of CPUs. All the L2 components are grouped as a class.
  94. *
  95. * When creating an MSC, struct mpam_msc is added to the all mpam_all_msc list,
  96. * then linked via struct mpam_ris to a vmsc, component and class.
  97. * The same MSC may exist under different class->component->vmsc paths, but the
  98. * RIS index will be unique.
  99. */
  100. LIST_HEAD(mpam_classes);
  101. /* List of all objects that can be free()d after synchronise_srcu() */
  102. static LLIST_HEAD(mpam_garbage);
  103. static inline void init_garbage(struct mpam_garbage *garbage)
  104. {
  105. init_llist_node(&garbage->llist);
  106. }
  107. #define add_to_garbage(x) \
  108. do { \
  109. __typeof__(x) _x = (x); \
  110. _x->garbage.to_free = _x; \
  111. llist_add(&_x->garbage.llist, &mpam_garbage); \
  112. } while (0)
  113. static void mpam_free_garbage(void)
  114. {
  115. struct mpam_garbage *iter, *tmp;
  116. struct llist_node *to_free = llist_del_all(&mpam_garbage);
  117. if (!to_free)
  118. return;
  119. synchronize_srcu(&mpam_srcu);
  120. llist_for_each_entry_safe(iter, tmp, to_free, llist) {
  121. if (iter->pdev)
  122. devm_kfree(&iter->pdev->dev, iter->to_free);
  123. else
  124. kfree(iter->to_free);
  125. }
  126. }
  127. /*
  128. * Once mpam is enabled, new requestors cannot further reduce the available
  129. * partid. Assert that the size is fixed, and new requestors will be turned
  130. * away.
  131. */
  132. static void mpam_assert_partid_sizes_fixed(void)
  133. {
  134. WARN_ON_ONCE(!partid_max_published);
  135. }
  136. static u32 __mpam_read_reg(struct mpam_msc *msc, u16 reg)
  137. {
  138. WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
  139. return readl_relaxed(msc->mapped_hwpage + reg);
  140. }
  141. static inline u32 _mpam_read_partsel_reg(struct mpam_msc *msc, u16 reg)
  142. {
  143. lockdep_assert_held_once(&msc->part_sel_lock);
  144. return __mpam_read_reg(msc, reg);
  145. }
  146. #define mpam_read_partsel_reg(msc, reg) _mpam_read_partsel_reg(msc, MPAMF_##reg)
  147. static void __mpam_write_reg(struct mpam_msc *msc, u16 reg, u32 val)
  148. {
  149. WARN_ON_ONCE(reg + sizeof(u32) > msc->mapped_hwpage_sz);
  150. WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
  151. writel_relaxed(val, msc->mapped_hwpage + reg);
  152. }
  153. static inline void _mpam_write_partsel_reg(struct mpam_msc *msc, u16 reg, u32 val)
  154. {
  155. lockdep_assert_held_once(&msc->part_sel_lock);
  156. __mpam_write_reg(msc, reg, val);
  157. }
  158. #define mpam_write_partsel_reg(msc, reg, val) _mpam_write_partsel_reg(msc, MPAMCFG_##reg, val)
  159. static inline u32 _mpam_read_monsel_reg(struct mpam_msc *msc, u16 reg)
  160. {
  161. mpam_mon_sel_lock_held(msc);
  162. return __mpam_read_reg(msc, reg);
  163. }
  164. #define mpam_read_monsel_reg(msc, reg) _mpam_read_monsel_reg(msc, MSMON_##reg)
  165. static inline void _mpam_write_monsel_reg(struct mpam_msc *msc, u16 reg, u32 val)
  166. {
  167. mpam_mon_sel_lock_held(msc);
  168. __mpam_write_reg(msc, reg, val);
  169. }
  170. #define mpam_write_monsel_reg(msc, reg, val) _mpam_write_monsel_reg(msc, MSMON_##reg, val)
  171. static u64 mpam_msc_read_idr(struct mpam_msc *msc)
  172. {
  173. u64 idr_high = 0, idr_low;
  174. lockdep_assert_held(&msc->part_sel_lock);
  175. idr_low = mpam_read_partsel_reg(msc, IDR);
  176. if (FIELD_GET(MPAMF_IDR_EXT, idr_low))
  177. idr_high = mpam_read_partsel_reg(msc, IDR + 4);
  178. return (idr_high << 32) | idr_low;
  179. }
  180. static void mpam_msc_clear_esr(struct mpam_msc *msc)
  181. {
  182. u64 esr_low = __mpam_read_reg(msc, MPAMF_ESR);
  183. if (!esr_low)
  184. return;
  185. /*
  186. * Clearing the high/low bits of MPAMF_ESR can not be atomic.
  187. * Clear the top half first, so that the pending error bits in the
  188. * lower half prevent hardware from updating either half of the
  189. * register.
  190. */
  191. if (msc->has_extd_esr)
  192. __mpam_write_reg(msc, MPAMF_ESR + 4, 0);
  193. __mpam_write_reg(msc, MPAMF_ESR, 0);
  194. }
  195. static u64 mpam_msc_read_esr(struct mpam_msc *msc)
  196. {
  197. u64 esr_high = 0, esr_low;
  198. esr_low = __mpam_read_reg(msc, MPAMF_ESR);
  199. if (msc->has_extd_esr)
  200. esr_high = __mpam_read_reg(msc, MPAMF_ESR + 4);
  201. return (esr_high << 32) | esr_low;
  202. }
  203. static void __mpam_part_sel_raw(u32 partsel, struct mpam_msc *msc)
  204. {
  205. lockdep_assert_held(&msc->part_sel_lock);
  206. mpam_write_partsel_reg(msc, PART_SEL, partsel);
  207. }
  208. static void __mpam_part_sel(u8 ris_idx, u16 partid, struct mpam_msc *msc)
  209. {
  210. u32 partsel = FIELD_PREP(MPAMCFG_PART_SEL_RIS, ris_idx) |
  211. FIELD_PREP(MPAMCFG_PART_SEL_PARTID_SEL, partid);
  212. __mpam_part_sel_raw(partsel, msc);
  213. }
  214. static void __mpam_intpart_sel(u8 ris_idx, u16 intpartid, struct mpam_msc *msc)
  215. {
  216. u32 partsel = FIELD_PREP(MPAMCFG_PART_SEL_RIS, ris_idx) |
  217. FIELD_PREP(MPAMCFG_PART_SEL_PARTID_SEL, intpartid) |
  218. MPAMCFG_PART_SEL_INTERNAL;
  219. __mpam_part_sel_raw(partsel, msc);
  220. }
  221. int mpam_register_requestor(u16 partid_max, u8 pmg_max)
  222. {
  223. guard(spinlock)(&partid_max_lock);
  224. if (!partid_max_init) {
  225. mpam_partid_max = partid_max;
  226. mpam_pmg_max = pmg_max;
  227. partid_max_init = true;
  228. } else if (!partid_max_published) {
  229. mpam_partid_max = min(mpam_partid_max, partid_max);
  230. mpam_pmg_max = min(mpam_pmg_max, pmg_max);
  231. } else {
  232. /* New requestors can't lower the values */
  233. if (partid_max < mpam_partid_max || pmg_max < mpam_pmg_max)
  234. return -EBUSY;
  235. }
  236. return 0;
  237. }
  238. EXPORT_SYMBOL(mpam_register_requestor);
  239. static struct mpam_class *
  240. mpam_class_alloc(u8 level_idx, enum mpam_class_types type)
  241. {
  242. struct mpam_class *class;
  243. lockdep_assert_held(&mpam_list_lock);
  244. class = kzalloc_obj(*class);
  245. if (!class)
  246. return ERR_PTR(-ENOMEM);
  247. init_garbage(&class->garbage);
  248. INIT_LIST_HEAD_RCU(&class->components);
  249. /* Affinity is updated when ris are added */
  250. class->level = level_idx;
  251. class->type = type;
  252. INIT_LIST_HEAD_RCU(&class->classes_list);
  253. ida_init(&class->ida_csu_mon);
  254. ida_init(&class->ida_mbwu_mon);
  255. list_add_rcu(&class->classes_list, &mpam_classes);
  256. return class;
  257. }
  258. static void mpam_class_destroy(struct mpam_class *class)
  259. {
  260. lockdep_assert_held(&mpam_list_lock);
  261. list_del_rcu(&class->classes_list);
  262. add_to_garbage(class);
  263. }
  264. static struct mpam_class *
  265. mpam_class_find(u8 level_idx, enum mpam_class_types type)
  266. {
  267. struct mpam_class *class;
  268. lockdep_assert_held(&mpam_list_lock);
  269. list_for_each_entry(class, &mpam_classes, classes_list) {
  270. if (class->type == type && class->level == level_idx)
  271. return class;
  272. }
  273. return mpam_class_alloc(level_idx, type);
  274. }
  275. static struct mpam_component *
  276. mpam_component_alloc(struct mpam_class *class, int id)
  277. {
  278. struct mpam_component *comp;
  279. lockdep_assert_held(&mpam_list_lock);
  280. comp = kzalloc_obj(*comp);
  281. if (!comp)
  282. return ERR_PTR(-ENOMEM);
  283. init_garbage(&comp->garbage);
  284. comp->comp_id = id;
  285. INIT_LIST_HEAD_RCU(&comp->vmsc);
  286. /* Affinity is updated when RIS are added */
  287. INIT_LIST_HEAD_RCU(&comp->class_list);
  288. comp->class = class;
  289. list_add_rcu(&comp->class_list, &class->components);
  290. return comp;
  291. }
  292. static void __destroy_component_cfg(struct mpam_component *comp);
  293. static void mpam_component_destroy(struct mpam_component *comp)
  294. {
  295. struct mpam_class *class = comp->class;
  296. lockdep_assert_held(&mpam_list_lock);
  297. __destroy_component_cfg(comp);
  298. list_del_rcu(&comp->class_list);
  299. add_to_garbage(comp);
  300. if (list_empty(&class->components))
  301. mpam_class_destroy(class);
  302. }
  303. static struct mpam_component *
  304. mpam_component_find(struct mpam_class *class, int id)
  305. {
  306. struct mpam_component *comp;
  307. lockdep_assert_held(&mpam_list_lock);
  308. list_for_each_entry(comp, &class->components, class_list) {
  309. if (comp->comp_id == id)
  310. return comp;
  311. }
  312. return mpam_component_alloc(class, id);
  313. }
  314. static struct mpam_vmsc *
  315. mpam_vmsc_alloc(struct mpam_component *comp, struct mpam_msc *msc)
  316. {
  317. struct mpam_vmsc *vmsc;
  318. lockdep_assert_held(&mpam_list_lock);
  319. vmsc = kzalloc_obj(*vmsc);
  320. if (!vmsc)
  321. return ERR_PTR(-ENOMEM);
  322. init_garbage(&vmsc->garbage);
  323. INIT_LIST_HEAD_RCU(&vmsc->ris);
  324. INIT_LIST_HEAD_RCU(&vmsc->comp_list);
  325. vmsc->comp = comp;
  326. vmsc->msc = msc;
  327. list_add_rcu(&vmsc->comp_list, &comp->vmsc);
  328. return vmsc;
  329. }
  330. static void mpam_vmsc_destroy(struct mpam_vmsc *vmsc)
  331. {
  332. struct mpam_component *comp = vmsc->comp;
  333. lockdep_assert_held(&mpam_list_lock);
  334. list_del_rcu(&vmsc->comp_list);
  335. add_to_garbage(vmsc);
  336. if (list_empty(&comp->vmsc))
  337. mpam_component_destroy(comp);
  338. }
  339. static struct mpam_vmsc *
  340. mpam_vmsc_find(struct mpam_component *comp, struct mpam_msc *msc)
  341. {
  342. struct mpam_vmsc *vmsc;
  343. lockdep_assert_held(&mpam_list_lock);
  344. list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
  345. if (vmsc->msc->id == msc->id)
  346. return vmsc;
  347. }
  348. return mpam_vmsc_alloc(comp, msc);
  349. }
  350. /*
  351. * The cacheinfo structures are only populated when CPUs are online.
  352. * This helper walks the acpi tables to include offline CPUs too.
  353. */
  354. int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
  355. cpumask_t *affinity)
  356. {
  357. return acpi_pptt_get_cpumask_from_cache_id(cache_id, affinity);
  358. }
  359. /*
  360. * cpumask_of_node() only knows about online CPUs. This can't tell us whether
  361. * a class is represented on all possible CPUs.
  362. */
  363. static void get_cpumask_from_node_id(u32 node_id, cpumask_t *affinity)
  364. {
  365. int cpu;
  366. for_each_possible_cpu(cpu) {
  367. if (node_id == cpu_to_node(cpu))
  368. cpumask_set_cpu(cpu, affinity);
  369. }
  370. }
  371. static int mpam_ris_get_affinity(struct mpam_msc *msc, cpumask_t *affinity,
  372. enum mpam_class_types type,
  373. struct mpam_class *class,
  374. struct mpam_component *comp)
  375. {
  376. int err;
  377. switch (type) {
  378. case MPAM_CLASS_CACHE:
  379. err = mpam_get_cpumask_from_cache_id(comp->comp_id, class->level,
  380. affinity);
  381. if (err) {
  382. dev_warn_once(&msc->pdev->dev,
  383. "Failed to determine CPU affinity\n");
  384. return err;
  385. }
  386. if (cpumask_empty(affinity))
  387. dev_warn_once(&msc->pdev->dev, "no CPUs associated with cache node\n");
  388. break;
  389. case MPAM_CLASS_MEMORY:
  390. get_cpumask_from_node_id(comp->comp_id, affinity);
  391. /* affinity may be empty for CPU-less memory nodes */
  392. break;
  393. case MPAM_CLASS_UNKNOWN:
  394. return 0;
  395. }
  396. cpumask_and(affinity, affinity, &msc->accessibility);
  397. return 0;
  398. }
  399. static int mpam_ris_create_locked(struct mpam_msc *msc, u8 ris_idx,
  400. enum mpam_class_types type, u8 class_id,
  401. int component_id)
  402. {
  403. int err;
  404. struct mpam_vmsc *vmsc;
  405. struct mpam_msc_ris *ris;
  406. struct mpam_class *class;
  407. struct mpam_component *comp;
  408. struct platform_device *pdev = msc->pdev;
  409. lockdep_assert_held(&mpam_list_lock);
  410. if (ris_idx > MPAM_MSC_MAX_NUM_RIS)
  411. return -EINVAL;
  412. if (test_and_set_bit(ris_idx, &msc->ris_idxs))
  413. return -EBUSY;
  414. ris = devm_kzalloc(&msc->pdev->dev, sizeof(*ris), GFP_KERNEL);
  415. if (!ris)
  416. return -ENOMEM;
  417. init_garbage(&ris->garbage);
  418. ris->garbage.pdev = pdev;
  419. class = mpam_class_find(class_id, type);
  420. if (IS_ERR(class))
  421. return PTR_ERR(class);
  422. comp = mpam_component_find(class, component_id);
  423. if (IS_ERR(comp)) {
  424. if (list_empty(&class->components))
  425. mpam_class_destroy(class);
  426. return PTR_ERR(comp);
  427. }
  428. vmsc = mpam_vmsc_find(comp, msc);
  429. if (IS_ERR(vmsc)) {
  430. if (list_empty(&comp->vmsc))
  431. mpam_component_destroy(comp);
  432. return PTR_ERR(vmsc);
  433. }
  434. err = mpam_ris_get_affinity(msc, &ris->affinity, type, class, comp);
  435. if (err) {
  436. if (list_empty(&vmsc->ris))
  437. mpam_vmsc_destroy(vmsc);
  438. return err;
  439. }
  440. ris->ris_idx = ris_idx;
  441. INIT_LIST_HEAD_RCU(&ris->msc_list);
  442. INIT_LIST_HEAD_RCU(&ris->vmsc_list);
  443. ris->vmsc = vmsc;
  444. cpumask_or(&comp->affinity, &comp->affinity, &ris->affinity);
  445. cpumask_or(&class->affinity, &class->affinity, &ris->affinity);
  446. list_add_rcu(&ris->vmsc_list, &vmsc->ris);
  447. list_add_rcu(&ris->msc_list, &msc->ris);
  448. return 0;
  449. }
  450. static void mpam_ris_destroy(struct mpam_msc_ris *ris)
  451. {
  452. struct mpam_vmsc *vmsc = ris->vmsc;
  453. struct mpam_msc *msc = vmsc->msc;
  454. struct mpam_component *comp = vmsc->comp;
  455. struct mpam_class *class = comp->class;
  456. lockdep_assert_held(&mpam_list_lock);
  457. /*
  458. * It is assumed affinities don't overlap. If they do the class becomes
  459. * unusable immediately.
  460. */
  461. cpumask_andnot(&class->affinity, &class->affinity, &ris->affinity);
  462. cpumask_andnot(&comp->affinity, &comp->affinity, &ris->affinity);
  463. clear_bit(ris->ris_idx, &msc->ris_idxs);
  464. list_del_rcu(&ris->msc_list);
  465. list_del_rcu(&ris->vmsc_list);
  466. add_to_garbage(ris);
  467. if (list_empty(&vmsc->ris))
  468. mpam_vmsc_destroy(vmsc);
  469. }
  470. int mpam_ris_create(struct mpam_msc *msc, u8 ris_idx,
  471. enum mpam_class_types type, u8 class_id, int component_id)
  472. {
  473. int err;
  474. mutex_lock(&mpam_list_lock);
  475. err = mpam_ris_create_locked(msc, ris_idx, type, class_id,
  476. component_id);
  477. mutex_unlock(&mpam_list_lock);
  478. if (err)
  479. mpam_free_garbage();
  480. return err;
  481. }
  482. static struct mpam_msc_ris *mpam_get_or_create_ris(struct mpam_msc *msc,
  483. u8 ris_idx)
  484. {
  485. int err;
  486. struct mpam_msc_ris *ris;
  487. lockdep_assert_held(&mpam_list_lock);
  488. if (!test_bit(ris_idx, &msc->ris_idxs)) {
  489. err = mpam_ris_create_locked(msc, ris_idx, MPAM_CLASS_UNKNOWN,
  490. 0, 0);
  491. if (err)
  492. return ERR_PTR(err);
  493. }
  494. list_for_each_entry(ris, &msc->ris, msc_list) {
  495. if (ris->ris_idx == ris_idx)
  496. return ris;
  497. }
  498. return ERR_PTR(-ENOENT);
  499. }
  500. /*
  501. * IHI009A.a has this nugget: "If a monitor does not support automatic behaviour
  502. * of NRDY, software can use this bit for any purpose" - so hardware might not
  503. * implement this - but it isn't RES0.
  504. *
  505. * Try and see what values stick in this bit. If we can write either value,
  506. * its probably not implemented by hardware.
  507. */
  508. static bool _mpam_ris_hw_probe_hw_nrdy(struct mpam_msc_ris *ris, u32 mon_reg)
  509. {
  510. u32 now;
  511. u64 mon_sel;
  512. bool can_set, can_clear;
  513. struct mpam_msc *msc = ris->vmsc->msc;
  514. if (WARN_ON_ONCE(!mpam_mon_sel_lock(msc)))
  515. return false;
  516. mon_sel = FIELD_PREP(MSMON_CFG_MON_SEL_MON_SEL, 0) |
  517. FIELD_PREP(MSMON_CFG_MON_SEL_RIS, ris->ris_idx);
  518. _mpam_write_monsel_reg(msc, mon_reg, mon_sel);
  519. _mpam_write_monsel_reg(msc, mon_reg, MSMON___NRDY);
  520. now = _mpam_read_monsel_reg(msc, mon_reg);
  521. can_set = now & MSMON___NRDY;
  522. _mpam_write_monsel_reg(msc, mon_reg, 0);
  523. now = _mpam_read_monsel_reg(msc, mon_reg);
  524. can_clear = !(now & MSMON___NRDY);
  525. mpam_mon_sel_unlock(msc);
  526. return (!can_set || !can_clear);
  527. }
  528. #define mpam_ris_hw_probe_hw_nrdy(_ris, _mon_reg) \
  529. _mpam_ris_hw_probe_hw_nrdy(_ris, MSMON_##_mon_reg)
  530. static void mpam_ris_hw_probe(struct mpam_msc_ris *ris)
  531. {
  532. int err;
  533. struct mpam_msc *msc = ris->vmsc->msc;
  534. struct device *dev = &msc->pdev->dev;
  535. struct mpam_props *props = &ris->props;
  536. struct mpam_class *class = ris->vmsc->comp->class;
  537. lockdep_assert_held(&msc->probe_lock);
  538. lockdep_assert_held(&msc->part_sel_lock);
  539. /* Cache Capacity Partitioning */
  540. if (FIELD_GET(MPAMF_IDR_HAS_CCAP_PART, ris->idr)) {
  541. u32 ccap_features = mpam_read_partsel_reg(msc, CCAP_IDR);
  542. props->cmax_wd = FIELD_GET(MPAMF_CCAP_IDR_CMAX_WD, ccap_features);
  543. if (props->cmax_wd &&
  544. FIELD_GET(MPAMF_CCAP_IDR_HAS_CMAX_SOFTLIM, ccap_features))
  545. mpam_set_feature(mpam_feat_cmax_softlim, props);
  546. if (props->cmax_wd &&
  547. !FIELD_GET(MPAMF_CCAP_IDR_NO_CMAX, ccap_features))
  548. mpam_set_feature(mpam_feat_cmax_cmax, props);
  549. if (props->cmax_wd &&
  550. FIELD_GET(MPAMF_CCAP_IDR_HAS_CMIN, ccap_features))
  551. mpam_set_feature(mpam_feat_cmax_cmin, props);
  552. props->cassoc_wd = FIELD_GET(MPAMF_CCAP_IDR_CASSOC_WD, ccap_features);
  553. if (props->cassoc_wd &&
  554. FIELD_GET(MPAMF_CCAP_IDR_HAS_CASSOC, ccap_features))
  555. mpam_set_feature(mpam_feat_cmax_cassoc, props);
  556. }
  557. /* Cache Portion partitioning */
  558. if (FIELD_GET(MPAMF_IDR_HAS_CPOR_PART, ris->idr)) {
  559. u32 cpor_features = mpam_read_partsel_reg(msc, CPOR_IDR);
  560. props->cpbm_wd = FIELD_GET(MPAMF_CPOR_IDR_CPBM_WD, cpor_features);
  561. if (props->cpbm_wd)
  562. mpam_set_feature(mpam_feat_cpor_part, props);
  563. }
  564. /* Memory bandwidth partitioning */
  565. if (FIELD_GET(MPAMF_IDR_HAS_MBW_PART, ris->idr)) {
  566. u32 mbw_features = mpam_read_partsel_reg(msc, MBW_IDR);
  567. /* portion bitmap resolution */
  568. props->mbw_pbm_bits = FIELD_GET(MPAMF_MBW_IDR_BWPBM_WD, mbw_features);
  569. if (props->mbw_pbm_bits &&
  570. FIELD_GET(MPAMF_MBW_IDR_HAS_PBM, mbw_features))
  571. mpam_set_feature(mpam_feat_mbw_part, props);
  572. props->bwa_wd = FIELD_GET(MPAMF_MBW_IDR_BWA_WD, mbw_features);
  573. if (props->bwa_wd && FIELD_GET(MPAMF_MBW_IDR_HAS_MAX, mbw_features))
  574. mpam_set_feature(mpam_feat_mbw_max, props);
  575. if (props->bwa_wd && FIELD_GET(MPAMF_MBW_IDR_HAS_MIN, mbw_features))
  576. mpam_set_feature(mpam_feat_mbw_min, props);
  577. if (props->bwa_wd && FIELD_GET(MPAMF_MBW_IDR_HAS_PROP, mbw_features))
  578. mpam_set_feature(mpam_feat_mbw_prop, props);
  579. }
  580. /* Priority partitioning */
  581. if (FIELD_GET(MPAMF_IDR_HAS_PRI_PART, ris->idr)) {
  582. u32 pri_features = mpam_read_partsel_reg(msc, PRI_IDR);
  583. props->intpri_wd = FIELD_GET(MPAMF_PRI_IDR_INTPRI_WD, pri_features);
  584. if (props->intpri_wd && FIELD_GET(MPAMF_PRI_IDR_HAS_INTPRI, pri_features)) {
  585. mpam_set_feature(mpam_feat_intpri_part, props);
  586. if (FIELD_GET(MPAMF_PRI_IDR_INTPRI_0_IS_LOW, pri_features))
  587. mpam_set_feature(mpam_feat_intpri_part_0_low, props);
  588. }
  589. props->dspri_wd = FIELD_GET(MPAMF_PRI_IDR_DSPRI_WD, pri_features);
  590. if (props->dspri_wd && FIELD_GET(MPAMF_PRI_IDR_HAS_DSPRI, pri_features)) {
  591. mpam_set_feature(mpam_feat_dspri_part, props);
  592. if (FIELD_GET(MPAMF_PRI_IDR_DSPRI_0_IS_LOW, pri_features))
  593. mpam_set_feature(mpam_feat_dspri_part_0_low, props);
  594. }
  595. }
  596. /* Performance Monitoring */
  597. if (FIELD_GET(MPAMF_IDR_HAS_MSMON, ris->idr)) {
  598. u32 msmon_features = mpam_read_partsel_reg(msc, MSMON_IDR);
  599. /*
  600. * If the firmware max-nrdy-us property is missing, the
  601. * CSU counters can't be used. Should we wait forever?
  602. */
  603. err = device_property_read_u32(&msc->pdev->dev,
  604. "arm,not-ready-us",
  605. &msc->nrdy_usec);
  606. if (FIELD_GET(MPAMF_MSMON_IDR_MSMON_CSU, msmon_features)) {
  607. u32 csumonidr;
  608. csumonidr = mpam_read_partsel_reg(msc, CSUMON_IDR);
  609. props->num_csu_mon = FIELD_GET(MPAMF_CSUMON_IDR_NUM_MON, csumonidr);
  610. if (props->num_csu_mon) {
  611. bool hw_managed;
  612. mpam_set_feature(mpam_feat_msmon_csu, props);
  613. if (FIELD_GET(MPAMF_CSUMON_IDR_HAS_XCL, csumonidr))
  614. mpam_set_feature(mpam_feat_msmon_csu_xcl, props);
  615. /* Is NRDY hardware managed? */
  616. hw_managed = mpam_ris_hw_probe_hw_nrdy(ris, CSU);
  617. if (hw_managed)
  618. mpam_set_feature(mpam_feat_msmon_csu_hw_nrdy, props);
  619. }
  620. /*
  621. * Accept the missing firmware property if NRDY appears
  622. * un-implemented.
  623. */
  624. if (err && mpam_has_feature(mpam_feat_msmon_csu_hw_nrdy, props))
  625. dev_err_once(dev, "Counters are not usable because not-ready timeout was not provided by firmware.");
  626. }
  627. if (FIELD_GET(MPAMF_MSMON_IDR_MSMON_MBWU, msmon_features)) {
  628. bool has_long, hw_managed;
  629. u32 mbwumon_idr = mpam_read_partsel_reg(msc, MBWUMON_IDR);
  630. props->num_mbwu_mon = FIELD_GET(MPAMF_MBWUMON_IDR_NUM_MON, mbwumon_idr);
  631. if (props->num_mbwu_mon) {
  632. mpam_set_feature(mpam_feat_msmon_mbwu, props);
  633. if (FIELD_GET(MPAMF_MBWUMON_IDR_HAS_RWBW, mbwumon_idr))
  634. mpam_set_feature(mpam_feat_msmon_mbwu_rwbw, props);
  635. has_long = FIELD_GET(MPAMF_MBWUMON_IDR_HAS_LONG, mbwumon_idr);
  636. if (has_long) {
  637. if (FIELD_GET(MPAMF_MBWUMON_IDR_LWD, mbwumon_idr))
  638. mpam_set_feature(mpam_feat_msmon_mbwu_63counter, props);
  639. else
  640. mpam_set_feature(mpam_feat_msmon_mbwu_44counter, props);
  641. } else {
  642. mpam_set_feature(mpam_feat_msmon_mbwu_31counter, props);
  643. }
  644. /* Is NRDY hardware managed? */
  645. hw_managed = mpam_ris_hw_probe_hw_nrdy(ris, MBWU);
  646. if (hw_managed)
  647. mpam_set_feature(mpam_feat_msmon_mbwu_hw_nrdy, props);
  648. /*
  649. * Don't warn about any missing firmware property for
  650. * MBWU NRDY - it doesn't make any sense!
  651. */
  652. }
  653. }
  654. }
  655. /*
  656. * RIS with PARTID narrowing don't have enough storage for one
  657. * configuration per PARTID. If these are in a class we could use,
  658. * reduce the supported partid_max to match the number of intpartid.
  659. * If the class is unknown, just ignore it.
  660. */
  661. if (FIELD_GET(MPAMF_IDR_HAS_PARTID_NRW, ris->idr) &&
  662. class->type != MPAM_CLASS_UNKNOWN) {
  663. u32 nrwidr = mpam_read_partsel_reg(msc, PARTID_NRW_IDR);
  664. u16 partid_max = FIELD_GET(MPAMF_PARTID_NRW_IDR_INTPARTID_MAX, nrwidr);
  665. mpam_set_feature(mpam_feat_partid_nrw, props);
  666. msc->partid_max = min(msc->partid_max, partid_max);
  667. }
  668. }
  669. static int mpam_msc_hw_probe(struct mpam_msc *msc)
  670. {
  671. u64 idr;
  672. u16 partid_max;
  673. u8 ris_idx, pmg_max;
  674. struct mpam_msc_ris *ris;
  675. struct device *dev = &msc->pdev->dev;
  676. lockdep_assert_held(&msc->probe_lock);
  677. idr = __mpam_read_reg(msc, MPAMF_AIDR);
  678. if ((idr & MPAMF_AIDR_ARCH_MAJOR_REV) != MPAM_ARCHITECTURE_V1) {
  679. dev_err_once(dev, "MSC does not match MPAM architecture v1.x\n");
  680. return -EIO;
  681. }
  682. /* Grab an IDR value to find out how many RIS there are */
  683. mutex_lock(&msc->part_sel_lock);
  684. idr = mpam_msc_read_idr(msc);
  685. mutex_unlock(&msc->part_sel_lock);
  686. msc->ris_max = FIELD_GET(MPAMF_IDR_RIS_MAX, idr);
  687. /* Use these values so partid/pmg always starts with a valid value */
  688. msc->partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr);
  689. msc->pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr);
  690. for (ris_idx = 0; ris_idx <= msc->ris_max; ris_idx++) {
  691. mutex_lock(&msc->part_sel_lock);
  692. __mpam_part_sel(ris_idx, 0, msc);
  693. idr = mpam_msc_read_idr(msc);
  694. mutex_unlock(&msc->part_sel_lock);
  695. partid_max = FIELD_GET(MPAMF_IDR_PARTID_MAX, idr);
  696. pmg_max = FIELD_GET(MPAMF_IDR_PMG_MAX, idr);
  697. msc->partid_max = min(msc->partid_max, partid_max);
  698. msc->pmg_max = min(msc->pmg_max, pmg_max);
  699. msc->has_extd_esr = FIELD_GET(MPAMF_IDR_HAS_EXTD_ESR, idr);
  700. mutex_lock(&mpam_list_lock);
  701. ris = mpam_get_or_create_ris(msc, ris_idx);
  702. mutex_unlock(&mpam_list_lock);
  703. if (IS_ERR(ris))
  704. return PTR_ERR(ris);
  705. ris->idr = idr;
  706. mutex_lock(&msc->part_sel_lock);
  707. __mpam_part_sel(ris_idx, 0, msc);
  708. mpam_ris_hw_probe(ris);
  709. mutex_unlock(&msc->part_sel_lock);
  710. }
  711. /* Clear any stale errors */
  712. mpam_msc_clear_esr(msc);
  713. spin_lock(&partid_max_lock);
  714. mpam_partid_max = min(mpam_partid_max, msc->partid_max);
  715. mpam_pmg_max = min(mpam_pmg_max, msc->pmg_max);
  716. spin_unlock(&partid_max_lock);
  717. msc->probed = true;
  718. return 0;
  719. }
  720. struct mon_read {
  721. struct mpam_msc_ris *ris;
  722. struct mon_cfg *ctx;
  723. enum mpam_device_features type;
  724. u64 *val;
  725. int err;
  726. };
  727. static bool mpam_ris_has_mbwu_long_counter(struct mpam_msc_ris *ris)
  728. {
  729. return (mpam_has_feature(mpam_feat_msmon_mbwu_63counter, &ris->props) ||
  730. mpam_has_feature(mpam_feat_msmon_mbwu_44counter, &ris->props));
  731. }
  732. static u64 mpam_msc_read_mbwu_l(struct mpam_msc *msc)
  733. {
  734. int retry = 3;
  735. u32 mbwu_l_low;
  736. u64 mbwu_l_high1, mbwu_l_high2;
  737. mpam_mon_sel_lock_held(msc);
  738. WARN_ON_ONCE((MSMON_MBWU_L + sizeof(u64)) > msc->mapped_hwpage_sz);
  739. WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
  740. mbwu_l_high2 = __mpam_read_reg(msc, MSMON_MBWU_L + 4);
  741. do {
  742. mbwu_l_high1 = mbwu_l_high2;
  743. mbwu_l_low = __mpam_read_reg(msc, MSMON_MBWU_L);
  744. mbwu_l_high2 = __mpam_read_reg(msc, MSMON_MBWU_L + 4);
  745. retry--;
  746. } while (mbwu_l_high1 != mbwu_l_high2 && retry > 0);
  747. if (mbwu_l_high1 == mbwu_l_high2)
  748. return (mbwu_l_high1 << 32) | mbwu_l_low;
  749. pr_warn("Failed to read a stable value\n");
  750. return MSMON___L_NRDY;
  751. }
  752. static void mpam_msc_zero_mbwu_l(struct mpam_msc *msc)
  753. {
  754. mpam_mon_sel_lock_held(msc);
  755. WARN_ON_ONCE((MSMON_MBWU_L + sizeof(u64)) > msc->mapped_hwpage_sz);
  756. WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(), &msc->accessibility));
  757. __mpam_write_reg(msc, MSMON_MBWU_L, 0);
  758. __mpam_write_reg(msc, MSMON_MBWU_L + 4, 0);
  759. }
  760. static void gen_msmon_ctl_flt_vals(struct mon_read *m, u32 *ctl_val,
  761. u32 *flt_val)
  762. {
  763. struct mon_cfg *ctx = m->ctx;
  764. /*
  765. * For CSU counters its implementation-defined what happens when not
  766. * filtering by partid.
  767. */
  768. *ctl_val = MSMON_CFG_x_CTL_MATCH_PARTID;
  769. *flt_val = FIELD_PREP(MSMON_CFG_x_FLT_PARTID, ctx->partid);
  770. if (m->ctx->match_pmg) {
  771. *ctl_val |= MSMON_CFG_x_CTL_MATCH_PMG;
  772. *flt_val |= FIELD_PREP(MSMON_CFG_x_FLT_PMG, ctx->pmg);
  773. }
  774. switch (m->type) {
  775. case mpam_feat_msmon_csu:
  776. *ctl_val |= MSMON_CFG_CSU_CTL_TYPE_CSU;
  777. if (mpam_has_feature(mpam_feat_msmon_csu_xcl, &m->ris->props))
  778. *flt_val |= FIELD_PREP(MSMON_CFG_CSU_FLT_XCL, ctx->csu_exclude_clean);
  779. break;
  780. case mpam_feat_msmon_mbwu_31counter:
  781. case mpam_feat_msmon_mbwu_44counter:
  782. case mpam_feat_msmon_mbwu_63counter:
  783. *ctl_val |= MSMON_CFG_MBWU_CTL_TYPE_MBWU;
  784. if (mpam_has_feature(mpam_feat_msmon_mbwu_rwbw, &m->ris->props))
  785. *flt_val |= FIELD_PREP(MSMON_CFG_MBWU_FLT_RWBW, ctx->opts);
  786. break;
  787. default:
  788. pr_warn("Unexpected monitor type %d\n", m->type);
  789. }
  790. }
  791. static void read_msmon_ctl_flt_vals(struct mon_read *m, u32 *ctl_val,
  792. u32 *flt_val)
  793. {
  794. struct mpam_msc *msc = m->ris->vmsc->msc;
  795. switch (m->type) {
  796. case mpam_feat_msmon_csu:
  797. *ctl_val = mpam_read_monsel_reg(msc, CFG_CSU_CTL);
  798. *flt_val = mpam_read_monsel_reg(msc, CFG_CSU_FLT);
  799. break;
  800. case mpam_feat_msmon_mbwu_31counter:
  801. case mpam_feat_msmon_mbwu_44counter:
  802. case mpam_feat_msmon_mbwu_63counter:
  803. *ctl_val = mpam_read_monsel_reg(msc, CFG_MBWU_CTL);
  804. *flt_val = mpam_read_monsel_reg(msc, CFG_MBWU_FLT);
  805. break;
  806. default:
  807. pr_warn("Unexpected monitor type %d\n", m->type);
  808. }
  809. }
  810. /* Remove values set by the hardware to prevent apparent mismatches. */
  811. static inline void clean_msmon_ctl_val(u32 *cur_ctl)
  812. {
  813. *cur_ctl &= ~MSMON_CFG_x_CTL_OFLOW_STATUS;
  814. if (FIELD_GET(MSMON_CFG_x_CTL_TYPE, *cur_ctl) == MSMON_CFG_MBWU_CTL_TYPE_MBWU)
  815. *cur_ctl &= ~MSMON_CFG_MBWU_CTL_OFLOW_STATUS_L;
  816. }
  817. static void write_msmon_ctl_flt_vals(struct mon_read *m, u32 ctl_val,
  818. u32 flt_val)
  819. {
  820. struct mpam_msc *msc = m->ris->vmsc->msc;
  821. /*
  822. * Write the ctl_val with the enable bit cleared, reset the counter,
  823. * then enable counter.
  824. */
  825. switch (m->type) {
  826. case mpam_feat_msmon_csu:
  827. mpam_write_monsel_reg(msc, CFG_CSU_FLT, flt_val);
  828. mpam_write_monsel_reg(msc, CFG_CSU_CTL, ctl_val);
  829. mpam_write_monsel_reg(msc, CSU, 0);
  830. mpam_write_monsel_reg(msc, CFG_CSU_CTL, ctl_val | MSMON_CFG_x_CTL_EN);
  831. break;
  832. case mpam_feat_msmon_mbwu_31counter:
  833. case mpam_feat_msmon_mbwu_44counter:
  834. case mpam_feat_msmon_mbwu_63counter:
  835. mpam_write_monsel_reg(msc, CFG_MBWU_FLT, flt_val);
  836. mpam_write_monsel_reg(msc, CFG_MBWU_CTL, ctl_val);
  837. mpam_write_monsel_reg(msc, CFG_MBWU_CTL, ctl_val | MSMON_CFG_x_CTL_EN);
  838. /* Counting monitors require NRDY to be reset by software */
  839. if (m->type == mpam_feat_msmon_mbwu_31counter)
  840. mpam_write_monsel_reg(msc, MBWU, 0);
  841. else
  842. mpam_msc_zero_mbwu_l(m->ris->vmsc->msc);
  843. break;
  844. default:
  845. pr_warn("Unexpected monitor type %d\n", m->type);
  846. }
  847. }
  848. static u64 mpam_msmon_overflow_val(enum mpam_device_features type)
  849. {
  850. /* TODO: implement scaling counters */
  851. switch (type) {
  852. case mpam_feat_msmon_mbwu_63counter:
  853. return BIT_ULL(hweight_long(MSMON___LWD_VALUE));
  854. case mpam_feat_msmon_mbwu_44counter:
  855. return BIT_ULL(hweight_long(MSMON___L_VALUE));
  856. case mpam_feat_msmon_mbwu_31counter:
  857. return BIT_ULL(hweight_long(MSMON___VALUE));
  858. default:
  859. return 0;
  860. }
  861. }
  862. static void __ris_msmon_read(void *arg)
  863. {
  864. u64 now;
  865. bool nrdy = false;
  866. bool config_mismatch;
  867. bool overflow = false;
  868. struct mon_read *m = arg;
  869. struct mon_cfg *ctx = m->ctx;
  870. bool reset_on_next_read = false;
  871. struct mpam_msc_ris *ris = m->ris;
  872. struct msmon_mbwu_state *mbwu_state;
  873. struct mpam_props *rprops = &ris->props;
  874. struct mpam_msc *msc = m->ris->vmsc->msc;
  875. u32 mon_sel, ctl_val, flt_val, cur_ctl, cur_flt;
  876. if (!mpam_mon_sel_lock(msc)) {
  877. m->err = -EIO;
  878. return;
  879. }
  880. mon_sel = FIELD_PREP(MSMON_CFG_MON_SEL_MON_SEL, ctx->mon) |
  881. FIELD_PREP(MSMON_CFG_MON_SEL_RIS, ris->ris_idx);
  882. mpam_write_monsel_reg(msc, CFG_MON_SEL, mon_sel);
  883. switch (m->type) {
  884. case mpam_feat_msmon_mbwu_31counter:
  885. case mpam_feat_msmon_mbwu_44counter:
  886. case mpam_feat_msmon_mbwu_63counter:
  887. mbwu_state = &ris->mbwu_state[ctx->mon];
  888. if (mbwu_state) {
  889. reset_on_next_read = mbwu_state->reset_on_next_read;
  890. mbwu_state->reset_on_next_read = false;
  891. }
  892. break;
  893. default:
  894. break;
  895. }
  896. /*
  897. * Read the existing configuration to avoid re-writing the same values.
  898. * This saves waiting for 'nrdy' on subsequent reads.
  899. */
  900. read_msmon_ctl_flt_vals(m, &cur_ctl, &cur_flt);
  901. if (mpam_feat_msmon_mbwu_31counter == m->type)
  902. overflow = cur_ctl & MSMON_CFG_x_CTL_OFLOW_STATUS;
  903. else if (mpam_feat_msmon_mbwu_44counter == m->type ||
  904. mpam_feat_msmon_mbwu_63counter == m->type)
  905. overflow = cur_ctl & MSMON_CFG_MBWU_CTL_OFLOW_STATUS_L;
  906. clean_msmon_ctl_val(&cur_ctl);
  907. gen_msmon_ctl_flt_vals(m, &ctl_val, &flt_val);
  908. config_mismatch = cur_flt != flt_val ||
  909. cur_ctl != (ctl_val | MSMON_CFG_x_CTL_EN);
  910. if (config_mismatch || reset_on_next_read) {
  911. write_msmon_ctl_flt_vals(m, ctl_val, flt_val);
  912. overflow = false;
  913. } else if (overflow) {
  914. mpam_write_monsel_reg(msc, CFG_MBWU_CTL,
  915. cur_ctl &
  916. ~(MSMON_CFG_x_CTL_OFLOW_STATUS |
  917. MSMON_CFG_MBWU_CTL_OFLOW_STATUS_L));
  918. }
  919. switch (m->type) {
  920. case mpam_feat_msmon_csu:
  921. now = mpam_read_monsel_reg(msc, CSU);
  922. if (mpam_has_feature(mpam_feat_msmon_csu_hw_nrdy, rprops))
  923. nrdy = now & MSMON___NRDY;
  924. now = FIELD_GET(MSMON___VALUE, now);
  925. break;
  926. case mpam_feat_msmon_mbwu_31counter:
  927. case mpam_feat_msmon_mbwu_44counter:
  928. case mpam_feat_msmon_mbwu_63counter:
  929. if (m->type != mpam_feat_msmon_mbwu_31counter) {
  930. now = mpam_msc_read_mbwu_l(msc);
  931. if (mpam_has_feature(mpam_feat_msmon_mbwu_hw_nrdy, rprops))
  932. nrdy = now & MSMON___L_NRDY;
  933. if (m->type == mpam_feat_msmon_mbwu_63counter)
  934. now = FIELD_GET(MSMON___LWD_VALUE, now);
  935. else
  936. now = FIELD_GET(MSMON___L_VALUE, now);
  937. } else {
  938. now = mpam_read_monsel_reg(msc, MBWU);
  939. if (mpam_has_feature(mpam_feat_msmon_mbwu_hw_nrdy, rprops))
  940. nrdy = now & MSMON___NRDY;
  941. now = FIELD_GET(MSMON___VALUE, now);
  942. }
  943. if (nrdy)
  944. break;
  945. mbwu_state = &ris->mbwu_state[ctx->mon];
  946. if (overflow)
  947. mbwu_state->correction += mpam_msmon_overflow_val(m->type);
  948. /*
  949. * Include bandwidth consumed before the last hardware reset and
  950. * a counter size increment for each overflow.
  951. */
  952. now += mbwu_state->correction;
  953. break;
  954. default:
  955. m->err = -EINVAL;
  956. }
  957. mpam_mon_sel_unlock(msc);
  958. if (nrdy)
  959. m->err = -EBUSY;
  960. if (m->err)
  961. return;
  962. *m->val += now;
  963. }
  964. static int _msmon_read(struct mpam_component *comp, struct mon_read *arg)
  965. {
  966. int err, any_err = 0;
  967. struct mpam_vmsc *vmsc;
  968. guard(srcu)(&mpam_srcu);
  969. list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
  970. srcu_read_lock_held(&mpam_srcu)) {
  971. struct mpam_msc *msc = vmsc->msc;
  972. struct mpam_msc_ris *ris;
  973. list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
  974. srcu_read_lock_held(&mpam_srcu)) {
  975. arg->ris = ris;
  976. err = smp_call_function_any(&msc->accessibility,
  977. __ris_msmon_read, arg,
  978. true);
  979. if (!err && arg->err)
  980. err = arg->err;
  981. /*
  982. * Save one error to be returned to the caller, but
  983. * keep reading counters so that get reprogrammed. On
  984. * platforms with NRDY this lets us wait once.
  985. */
  986. if (err)
  987. any_err = err;
  988. }
  989. }
  990. return any_err;
  991. }
  992. static enum mpam_device_features mpam_msmon_choose_counter(struct mpam_class *class)
  993. {
  994. struct mpam_props *cprops = &class->props;
  995. if (mpam_has_feature(mpam_feat_msmon_mbwu_63counter, cprops))
  996. return mpam_feat_msmon_mbwu_63counter;
  997. if (mpam_has_feature(mpam_feat_msmon_mbwu_44counter, cprops))
  998. return mpam_feat_msmon_mbwu_44counter;
  999. return mpam_feat_msmon_mbwu_31counter;
  1000. }
  1001. int mpam_msmon_read(struct mpam_component *comp, struct mon_cfg *ctx,
  1002. enum mpam_device_features type, u64 *val)
  1003. {
  1004. int err;
  1005. struct mon_read arg;
  1006. u64 wait_jiffies = 0;
  1007. struct mpam_class *class = comp->class;
  1008. struct mpam_props *cprops = &class->props;
  1009. might_sleep();
  1010. if (!mpam_is_enabled())
  1011. return -EIO;
  1012. if (!mpam_has_feature(type, cprops))
  1013. return -EOPNOTSUPP;
  1014. if (type == mpam_feat_msmon_mbwu)
  1015. type = mpam_msmon_choose_counter(class);
  1016. arg = (struct mon_read) {
  1017. .ctx = ctx,
  1018. .type = type,
  1019. .val = val,
  1020. };
  1021. *val = 0;
  1022. err = _msmon_read(comp, &arg);
  1023. if (err == -EBUSY && class->nrdy_usec)
  1024. wait_jiffies = usecs_to_jiffies(class->nrdy_usec);
  1025. while (wait_jiffies)
  1026. wait_jiffies = schedule_timeout_uninterruptible(wait_jiffies);
  1027. if (err == -EBUSY) {
  1028. arg = (struct mon_read) {
  1029. .ctx = ctx,
  1030. .type = type,
  1031. .val = val,
  1032. };
  1033. *val = 0;
  1034. err = _msmon_read(comp, &arg);
  1035. }
  1036. return err;
  1037. }
  1038. void mpam_msmon_reset_mbwu(struct mpam_component *comp, struct mon_cfg *ctx)
  1039. {
  1040. struct mpam_msc *msc;
  1041. struct mpam_vmsc *vmsc;
  1042. struct mpam_msc_ris *ris;
  1043. if (!mpam_is_enabled())
  1044. return;
  1045. guard(srcu)(&mpam_srcu);
  1046. list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
  1047. srcu_read_lock_held(&mpam_srcu)) {
  1048. if (!mpam_has_feature(mpam_feat_msmon_mbwu, &vmsc->props))
  1049. continue;
  1050. msc = vmsc->msc;
  1051. list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
  1052. srcu_read_lock_held(&mpam_srcu)) {
  1053. if (!mpam_has_feature(mpam_feat_msmon_mbwu, &ris->props))
  1054. continue;
  1055. if (WARN_ON_ONCE(!mpam_mon_sel_lock(msc)))
  1056. continue;
  1057. ris->mbwu_state[ctx->mon].correction = 0;
  1058. ris->mbwu_state[ctx->mon].reset_on_next_read = true;
  1059. mpam_mon_sel_unlock(msc);
  1060. }
  1061. }
  1062. }
  1063. static void mpam_reset_msc_bitmap(struct mpam_msc *msc, u16 reg, u16 wd)
  1064. {
  1065. u32 num_words, msb;
  1066. u32 bm = ~0;
  1067. int i;
  1068. lockdep_assert_held(&msc->part_sel_lock);
  1069. if (wd == 0)
  1070. return;
  1071. /*
  1072. * Write all ~0 to all but the last 32bit-word, which may
  1073. * have fewer bits...
  1074. */
  1075. num_words = DIV_ROUND_UP(wd, 32);
  1076. for (i = 0; i < num_words - 1; i++, reg += sizeof(bm))
  1077. __mpam_write_reg(msc, reg, bm);
  1078. /*
  1079. * ....and then the last (maybe) partial 32bit word. When wd is a
  1080. * multiple of 32, msb should be 31 to write a full 32bit word.
  1081. */
  1082. msb = (wd - 1) % 32;
  1083. bm = GENMASK(msb, 0);
  1084. __mpam_write_reg(msc, reg, bm);
  1085. }
  1086. /* Called via IPI. Call while holding an SRCU reference */
  1087. static void mpam_reprogram_ris_partid(struct mpam_msc_ris *ris, u16 partid,
  1088. struct mpam_config *cfg)
  1089. {
  1090. u32 pri_val = 0;
  1091. u16 cmax = MPAMCFG_CMAX_CMAX;
  1092. struct mpam_msc *msc = ris->vmsc->msc;
  1093. struct mpam_props *rprops = &ris->props;
  1094. u16 dspri = GENMASK(rprops->dspri_wd, 0);
  1095. u16 intpri = GENMASK(rprops->intpri_wd, 0);
  1096. mutex_lock(&msc->part_sel_lock);
  1097. __mpam_part_sel(ris->ris_idx, partid, msc);
  1098. if (mpam_has_feature(mpam_feat_partid_nrw, rprops)) {
  1099. /* Update the intpartid mapping */
  1100. mpam_write_partsel_reg(msc, INTPARTID,
  1101. MPAMCFG_INTPARTID_INTERNAL | partid);
  1102. /*
  1103. * Then switch to the 'internal' partid to update the
  1104. * configuration.
  1105. */
  1106. __mpam_intpart_sel(ris->ris_idx, partid, msc);
  1107. }
  1108. if (mpam_has_feature(mpam_feat_cpor_part, rprops) &&
  1109. mpam_has_feature(mpam_feat_cpor_part, cfg)) {
  1110. if (cfg->reset_cpbm)
  1111. mpam_reset_msc_bitmap(msc, MPAMCFG_CPBM, rprops->cpbm_wd);
  1112. else
  1113. mpam_write_partsel_reg(msc, CPBM, cfg->cpbm);
  1114. }
  1115. if (mpam_has_feature(mpam_feat_mbw_part, rprops) &&
  1116. mpam_has_feature(mpam_feat_mbw_part, cfg)) {
  1117. if (cfg->reset_mbw_pbm)
  1118. mpam_reset_msc_bitmap(msc, MPAMCFG_MBW_PBM, rprops->mbw_pbm_bits);
  1119. else
  1120. mpam_write_partsel_reg(msc, MBW_PBM, cfg->mbw_pbm);
  1121. }
  1122. if (mpam_has_feature(mpam_feat_mbw_min, rprops) &&
  1123. mpam_has_feature(mpam_feat_mbw_min, cfg))
  1124. mpam_write_partsel_reg(msc, MBW_MIN, 0);
  1125. if (mpam_has_feature(mpam_feat_mbw_max, rprops) &&
  1126. mpam_has_feature(mpam_feat_mbw_max, cfg)) {
  1127. if (cfg->reset_mbw_max)
  1128. mpam_write_partsel_reg(msc, MBW_MAX, MPAMCFG_MBW_MAX_MAX);
  1129. else
  1130. mpam_write_partsel_reg(msc, MBW_MAX, cfg->mbw_max);
  1131. }
  1132. if (mpam_has_feature(mpam_feat_mbw_prop, rprops) &&
  1133. mpam_has_feature(mpam_feat_mbw_prop, cfg))
  1134. mpam_write_partsel_reg(msc, MBW_PROP, 0);
  1135. if (mpam_has_feature(mpam_feat_cmax_cmax, rprops))
  1136. mpam_write_partsel_reg(msc, CMAX, cmax);
  1137. if (mpam_has_feature(mpam_feat_cmax_cmin, rprops))
  1138. mpam_write_partsel_reg(msc, CMIN, 0);
  1139. if (mpam_has_feature(mpam_feat_cmax_cassoc, rprops))
  1140. mpam_write_partsel_reg(msc, CASSOC, MPAMCFG_CASSOC_CASSOC);
  1141. if (mpam_has_feature(mpam_feat_intpri_part, rprops) ||
  1142. mpam_has_feature(mpam_feat_dspri_part, rprops)) {
  1143. /* aces high? */
  1144. if (!mpam_has_feature(mpam_feat_intpri_part_0_low, rprops))
  1145. intpri = 0;
  1146. if (!mpam_has_feature(mpam_feat_dspri_part_0_low, rprops))
  1147. dspri = 0;
  1148. if (mpam_has_feature(mpam_feat_intpri_part, rprops))
  1149. pri_val |= FIELD_PREP(MPAMCFG_PRI_INTPRI, intpri);
  1150. if (mpam_has_feature(mpam_feat_dspri_part, rprops))
  1151. pri_val |= FIELD_PREP(MPAMCFG_PRI_DSPRI, dspri);
  1152. mpam_write_partsel_reg(msc, PRI, pri_val);
  1153. }
  1154. mutex_unlock(&msc->part_sel_lock);
  1155. }
  1156. /* Call with msc cfg_lock held */
  1157. static int mpam_restore_mbwu_state(void *_ris)
  1158. {
  1159. int i;
  1160. u64 val;
  1161. struct mon_read mwbu_arg;
  1162. struct mpam_msc_ris *ris = _ris;
  1163. struct mpam_class *class = ris->vmsc->comp->class;
  1164. for (i = 0; i < ris->props.num_mbwu_mon; i++) {
  1165. if (ris->mbwu_state[i].enabled) {
  1166. mwbu_arg.ris = ris;
  1167. mwbu_arg.ctx = &ris->mbwu_state[i].cfg;
  1168. mwbu_arg.type = mpam_msmon_choose_counter(class);
  1169. mwbu_arg.val = &val;
  1170. __ris_msmon_read(&mwbu_arg);
  1171. }
  1172. }
  1173. return 0;
  1174. }
  1175. /* Call with MSC cfg_lock held */
  1176. static int mpam_save_mbwu_state(void *arg)
  1177. {
  1178. int i;
  1179. u64 val;
  1180. struct mon_cfg *cfg;
  1181. u32 cur_flt, cur_ctl, mon_sel;
  1182. struct mpam_msc_ris *ris = arg;
  1183. struct msmon_mbwu_state *mbwu_state;
  1184. struct mpam_msc *msc = ris->vmsc->msc;
  1185. for (i = 0; i < ris->props.num_mbwu_mon; i++) {
  1186. mbwu_state = &ris->mbwu_state[i];
  1187. cfg = &mbwu_state->cfg;
  1188. if (WARN_ON_ONCE(!mpam_mon_sel_lock(msc)))
  1189. return -EIO;
  1190. mon_sel = FIELD_PREP(MSMON_CFG_MON_SEL_MON_SEL, i) |
  1191. FIELD_PREP(MSMON_CFG_MON_SEL_RIS, ris->ris_idx);
  1192. mpam_write_monsel_reg(msc, CFG_MON_SEL, mon_sel);
  1193. cur_flt = mpam_read_monsel_reg(msc, CFG_MBWU_FLT);
  1194. cur_ctl = mpam_read_monsel_reg(msc, CFG_MBWU_CTL);
  1195. mpam_write_monsel_reg(msc, CFG_MBWU_CTL, 0);
  1196. if (mpam_ris_has_mbwu_long_counter(ris)) {
  1197. val = mpam_msc_read_mbwu_l(msc);
  1198. mpam_msc_zero_mbwu_l(msc);
  1199. } else {
  1200. val = mpam_read_monsel_reg(msc, MBWU);
  1201. mpam_write_monsel_reg(msc, MBWU, 0);
  1202. }
  1203. cfg->mon = i;
  1204. cfg->pmg = FIELD_GET(MSMON_CFG_x_FLT_PMG, cur_flt);
  1205. cfg->match_pmg = FIELD_GET(MSMON_CFG_x_CTL_MATCH_PMG, cur_ctl);
  1206. cfg->partid = FIELD_GET(MSMON_CFG_x_FLT_PARTID, cur_flt);
  1207. mbwu_state->correction += val;
  1208. mbwu_state->enabled = FIELD_GET(MSMON_CFG_x_CTL_EN, cur_ctl);
  1209. mpam_mon_sel_unlock(msc);
  1210. }
  1211. return 0;
  1212. }
  1213. static void mpam_init_reset_cfg(struct mpam_config *reset_cfg)
  1214. {
  1215. *reset_cfg = (struct mpam_config) {
  1216. .reset_cpbm = true,
  1217. .reset_mbw_pbm = true,
  1218. .reset_mbw_max = true,
  1219. };
  1220. bitmap_fill(reset_cfg->features, MPAM_FEATURE_LAST);
  1221. }
  1222. /*
  1223. * Called via smp_call_on_cpu() to prevent migration, while still being
  1224. * pre-emptible. Caller must hold mpam_srcu.
  1225. */
  1226. static int mpam_reset_ris(void *arg)
  1227. {
  1228. u16 partid, partid_max;
  1229. struct mpam_config reset_cfg;
  1230. struct mpam_msc_ris *ris = arg;
  1231. if (ris->in_reset_state)
  1232. return 0;
  1233. mpam_init_reset_cfg(&reset_cfg);
  1234. spin_lock(&partid_max_lock);
  1235. partid_max = mpam_partid_max;
  1236. spin_unlock(&partid_max_lock);
  1237. for (partid = 0; partid <= partid_max; partid++)
  1238. mpam_reprogram_ris_partid(ris, partid, &reset_cfg);
  1239. return 0;
  1240. }
  1241. /*
  1242. * Get the preferred CPU for this MSC. If it is accessible from this CPU,
  1243. * this CPU is preferred. This can be preempted/migrated, it will only result
  1244. * in more work.
  1245. */
  1246. static int mpam_get_msc_preferred_cpu(struct mpam_msc *msc)
  1247. {
  1248. int cpu = raw_smp_processor_id();
  1249. if (cpumask_test_cpu(cpu, &msc->accessibility))
  1250. return cpu;
  1251. return cpumask_first_and(&msc->accessibility, cpu_online_mask);
  1252. }
  1253. static int mpam_touch_msc(struct mpam_msc *msc, int (*fn)(void *a), void *arg)
  1254. {
  1255. lockdep_assert_irqs_enabled();
  1256. lockdep_assert_cpus_held();
  1257. WARN_ON_ONCE(!srcu_read_lock_held((&mpam_srcu)));
  1258. return smp_call_on_cpu(mpam_get_msc_preferred_cpu(msc), fn, arg, true);
  1259. }
  1260. struct mpam_write_config_arg {
  1261. struct mpam_msc_ris *ris;
  1262. struct mpam_component *comp;
  1263. u16 partid;
  1264. };
  1265. static int __write_config(void *arg)
  1266. {
  1267. struct mpam_write_config_arg *c = arg;
  1268. mpam_reprogram_ris_partid(c->ris, c->partid, &c->comp->cfg[c->partid]);
  1269. return 0;
  1270. }
  1271. static void mpam_reprogram_msc(struct mpam_msc *msc)
  1272. {
  1273. u16 partid;
  1274. bool reset;
  1275. struct mpam_config *cfg;
  1276. struct mpam_msc_ris *ris;
  1277. struct mpam_write_config_arg arg;
  1278. /*
  1279. * No lock for mpam_partid_max as partid_max_published has been
  1280. * set by mpam_enabled(), so the values can no longer change.
  1281. */
  1282. mpam_assert_partid_sizes_fixed();
  1283. mutex_lock(&msc->cfg_lock);
  1284. list_for_each_entry_srcu(ris, &msc->ris, msc_list,
  1285. srcu_read_lock_held(&mpam_srcu)) {
  1286. if (!mpam_is_enabled() && !ris->in_reset_state) {
  1287. mpam_touch_msc(msc, &mpam_reset_ris, ris);
  1288. ris->in_reset_state = true;
  1289. continue;
  1290. }
  1291. arg.comp = ris->vmsc->comp;
  1292. arg.ris = ris;
  1293. reset = true;
  1294. for (partid = 0; partid <= mpam_partid_max; partid++) {
  1295. cfg = &ris->vmsc->comp->cfg[partid];
  1296. if (!bitmap_empty(cfg->features, MPAM_FEATURE_LAST))
  1297. reset = false;
  1298. arg.partid = partid;
  1299. mpam_touch_msc(msc, __write_config, &arg);
  1300. }
  1301. ris->in_reset_state = reset;
  1302. if (mpam_has_feature(mpam_feat_msmon_mbwu, &ris->props))
  1303. mpam_touch_msc(msc, &mpam_restore_mbwu_state, ris);
  1304. }
  1305. mutex_unlock(&msc->cfg_lock);
  1306. }
  1307. static void _enable_percpu_irq(void *_irq)
  1308. {
  1309. int *irq = _irq;
  1310. enable_percpu_irq(*irq, IRQ_TYPE_NONE);
  1311. }
  1312. static int mpam_cpu_online(unsigned int cpu)
  1313. {
  1314. struct mpam_msc *msc;
  1315. guard(srcu)(&mpam_srcu);
  1316. list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
  1317. srcu_read_lock_held(&mpam_srcu)) {
  1318. if (!cpumask_test_cpu(cpu, &msc->accessibility))
  1319. continue;
  1320. if (msc->reenable_error_ppi)
  1321. _enable_percpu_irq(&msc->reenable_error_ppi);
  1322. if (atomic_fetch_inc(&msc->online_refs) == 0)
  1323. mpam_reprogram_msc(msc);
  1324. }
  1325. return 0;
  1326. }
  1327. /* Before mpam is enabled, try to probe new MSC */
  1328. static int mpam_discovery_cpu_online(unsigned int cpu)
  1329. {
  1330. int err = 0;
  1331. struct mpam_msc *msc;
  1332. bool new_device_probed = false;
  1333. if (mpam_is_enabled())
  1334. return 0;
  1335. guard(srcu)(&mpam_srcu);
  1336. list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
  1337. srcu_read_lock_held(&mpam_srcu)) {
  1338. if (!cpumask_test_cpu(cpu, &msc->accessibility))
  1339. continue;
  1340. mutex_lock(&msc->probe_lock);
  1341. if (!msc->probed)
  1342. err = mpam_msc_hw_probe(msc);
  1343. mutex_unlock(&msc->probe_lock);
  1344. if (err)
  1345. break;
  1346. new_device_probed = true;
  1347. }
  1348. if (new_device_probed && !err)
  1349. schedule_work(&mpam_enable_work);
  1350. if (err) {
  1351. mpam_disable_reason = "error during probing";
  1352. schedule_work(&mpam_broken_work);
  1353. }
  1354. return err;
  1355. }
  1356. static int mpam_cpu_offline(unsigned int cpu)
  1357. {
  1358. struct mpam_msc *msc;
  1359. guard(srcu)(&mpam_srcu);
  1360. list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
  1361. srcu_read_lock_held(&mpam_srcu)) {
  1362. if (!cpumask_test_cpu(cpu, &msc->accessibility))
  1363. continue;
  1364. if (msc->reenable_error_ppi)
  1365. disable_percpu_irq(msc->reenable_error_ppi);
  1366. if (atomic_dec_and_test(&msc->online_refs)) {
  1367. struct mpam_msc_ris *ris;
  1368. mutex_lock(&msc->cfg_lock);
  1369. list_for_each_entry_srcu(ris, &msc->ris, msc_list,
  1370. srcu_read_lock_held(&mpam_srcu)) {
  1371. mpam_touch_msc(msc, &mpam_reset_ris, ris);
  1372. /*
  1373. * The reset state for non-zero partid may be
  1374. * lost while the CPUs are offline.
  1375. */
  1376. ris->in_reset_state = false;
  1377. if (mpam_is_enabled())
  1378. mpam_touch_msc(msc, &mpam_save_mbwu_state, ris);
  1379. }
  1380. mutex_unlock(&msc->cfg_lock);
  1381. }
  1382. }
  1383. return 0;
  1384. }
  1385. static void mpam_register_cpuhp_callbacks(int (*online)(unsigned int online),
  1386. int (*offline)(unsigned int offline),
  1387. char *name)
  1388. {
  1389. mutex_lock(&mpam_cpuhp_state_lock);
  1390. if (mpam_cpuhp_state) {
  1391. cpuhp_remove_state(mpam_cpuhp_state);
  1392. mpam_cpuhp_state = 0;
  1393. }
  1394. mpam_cpuhp_state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, name, online,
  1395. offline);
  1396. if (mpam_cpuhp_state <= 0) {
  1397. pr_err("Failed to register cpuhp callbacks");
  1398. mpam_cpuhp_state = 0;
  1399. }
  1400. mutex_unlock(&mpam_cpuhp_state_lock);
  1401. }
  1402. static int __setup_ppi(struct mpam_msc *msc)
  1403. {
  1404. int cpu;
  1405. msc->error_dev_id = alloc_percpu(struct mpam_msc *);
  1406. if (!msc->error_dev_id)
  1407. return -ENOMEM;
  1408. for_each_cpu(cpu, &msc->accessibility)
  1409. *per_cpu_ptr(msc->error_dev_id, cpu) = msc;
  1410. return 0;
  1411. }
  1412. static int mpam_msc_setup_error_irq(struct mpam_msc *msc)
  1413. {
  1414. int irq;
  1415. irq = platform_get_irq_byname_optional(msc->pdev, "error");
  1416. if (irq <= 0)
  1417. return 0;
  1418. /* Allocate and initialise the percpu device pointer for PPI */
  1419. if (irq_is_percpu(irq))
  1420. return __setup_ppi(msc);
  1421. /* sanity check: shared interrupts can be routed anywhere? */
  1422. if (!cpumask_equal(&msc->accessibility, cpu_possible_mask)) {
  1423. pr_err_once("msc:%u is a private resource with a shared error interrupt",
  1424. msc->id);
  1425. return -EINVAL;
  1426. }
  1427. return 0;
  1428. }
  1429. /*
  1430. * An MSC can control traffic from a set of CPUs, but may only be accessible
  1431. * from a (hopefully wider) set of CPUs. The common reason for this is power
  1432. * management. If all the CPUs in a cluster are in PSCI:CPU_SUSPEND, the
  1433. * corresponding cache may also be powered off. By making accesses from
  1434. * one of those CPUs, we ensure we don't access a cache that's powered off.
  1435. */
  1436. static void update_msc_accessibility(struct mpam_msc *msc)
  1437. {
  1438. u32 affinity_id;
  1439. int err;
  1440. err = device_property_read_u32(&msc->pdev->dev, "cpu_affinity",
  1441. &affinity_id);
  1442. if (err)
  1443. cpumask_copy(&msc->accessibility, cpu_possible_mask);
  1444. else
  1445. acpi_pptt_get_cpus_from_container(affinity_id, &msc->accessibility);
  1446. }
  1447. /*
  1448. * There are two ways of reaching a struct mpam_msc_ris. Via the
  1449. * class->component->vmsc->ris, or via the msc.
  1450. * When destroying the msc, the other side needs unlinking and cleaning up too.
  1451. */
  1452. static void mpam_msc_destroy(struct mpam_msc *msc)
  1453. {
  1454. struct platform_device *pdev = msc->pdev;
  1455. struct mpam_msc_ris *ris, *tmp;
  1456. lockdep_assert_held(&mpam_list_lock);
  1457. list_for_each_entry_safe(ris, tmp, &msc->ris, msc_list)
  1458. mpam_ris_destroy(ris);
  1459. list_del_rcu(&msc->all_msc_list);
  1460. platform_set_drvdata(pdev, NULL);
  1461. add_to_garbage(msc);
  1462. }
  1463. static void mpam_msc_drv_remove(struct platform_device *pdev)
  1464. {
  1465. struct mpam_msc *msc = platform_get_drvdata(pdev);
  1466. mutex_lock(&mpam_list_lock);
  1467. mpam_msc_destroy(msc);
  1468. mutex_unlock(&mpam_list_lock);
  1469. mpam_free_garbage();
  1470. }
  1471. static struct mpam_msc *do_mpam_msc_drv_probe(struct platform_device *pdev)
  1472. {
  1473. int err;
  1474. u32 tmp;
  1475. struct mpam_msc *msc;
  1476. struct resource *msc_res;
  1477. struct device *dev = &pdev->dev;
  1478. lockdep_assert_held(&mpam_list_lock);
  1479. msc = devm_kzalloc(&pdev->dev, sizeof(*msc), GFP_KERNEL);
  1480. if (!msc)
  1481. return ERR_PTR(-ENOMEM);
  1482. init_garbage(&msc->garbage);
  1483. msc->garbage.pdev = pdev;
  1484. err = devm_mutex_init(dev, &msc->probe_lock);
  1485. if (err)
  1486. return ERR_PTR(err);
  1487. err = devm_mutex_init(dev, &msc->part_sel_lock);
  1488. if (err)
  1489. return ERR_PTR(err);
  1490. err = devm_mutex_init(dev, &msc->error_irq_lock);
  1491. if (err)
  1492. return ERR_PTR(err);
  1493. err = devm_mutex_init(dev, &msc->cfg_lock);
  1494. if (err)
  1495. return ERR_PTR(err);
  1496. mpam_mon_sel_lock_init(msc);
  1497. msc->id = pdev->id;
  1498. msc->pdev = pdev;
  1499. INIT_LIST_HEAD_RCU(&msc->all_msc_list);
  1500. INIT_LIST_HEAD_RCU(&msc->ris);
  1501. update_msc_accessibility(msc);
  1502. if (cpumask_empty(&msc->accessibility)) {
  1503. dev_err_once(dev, "MSC is not accessible from any CPU!");
  1504. return ERR_PTR(-EINVAL);
  1505. }
  1506. err = mpam_msc_setup_error_irq(msc);
  1507. if (err)
  1508. return ERR_PTR(err);
  1509. if (device_property_read_u32(&pdev->dev, "pcc-channel", &tmp))
  1510. msc->iface = MPAM_IFACE_MMIO;
  1511. else
  1512. msc->iface = MPAM_IFACE_PCC;
  1513. if (msc->iface == MPAM_IFACE_MMIO) {
  1514. void __iomem *io;
  1515. io = devm_platform_get_and_ioremap_resource(pdev, 0,
  1516. &msc_res);
  1517. if (IS_ERR(io)) {
  1518. dev_err_once(dev, "Failed to map MSC base address\n");
  1519. return ERR_CAST(io);
  1520. }
  1521. msc->mapped_hwpage_sz = msc_res->end - msc_res->start;
  1522. msc->mapped_hwpage = io;
  1523. } else {
  1524. return ERR_PTR(-EINVAL);
  1525. }
  1526. list_add_rcu(&msc->all_msc_list, &mpam_all_msc);
  1527. platform_set_drvdata(pdev, msc);
  1528. return msc;
  1529. }
  1530. static int fw_num_msc;
  1531. static int mpam_msc_drv_probe(struct platform_device *pdev)
  1532. {
  1533. int err;
  1534. struct mpam_msc *msc = NULL;
  1535. void *plat_data = pdev->dev.platform_data;
  1536. mutex_lock(&mpam_list_lock);
  1537. msc = do_mpam_msc_drv_probe(pdev);
  1538. mutex_unlock(&mpam_list_lock);
  1539. if (IS_ERR(msc))
  1540. return PTR_ERR(msc);
  1541. /* Create RIS entries described by firmware */
  1542. err = acpi_mpam_parse_resources(msc, plat_data);
  1543. if (err) {
  1544. mpam_msc_drv_remove(pdev);
  1545. return err;
  1546. }
  1547. if (atomic_add_return(1, &mpam_num_msc) == fw_num_msc)
  1548. mpam_register_cpuhp_callbacks(mpam_discovery_cpu_online, NULL,
  1549. "mpam:drv_probe");
  1550. return 0;
  1551. }
  1552. static struct platform_driver mpam_msc_driver = {
  1553. .driver = {
  1554. .name = "mpam_msc",
  1555. },
  1556. .probe = mpam_msc_drv_probe,
  1557. .remove = mpam_msc_drv_remove,
  1558. };
  1559. /* Any of these features mean the BWA_WD field is valid. */
  1560. static bool mpam_has_bwa_wd_feature(struct mpam_props *props)
  1561. {
  1562. if (mpam_has_feature(mpam_feat_mbw_min, props))
  1563. return true;
  1564. if (mpam_has_feature(mpam_feat_mbw_max, props))
  1565. return true;
  1566. if (mpam_has_feature(mpam_feat_mbw_prop, props))
  1567. return true;
  1568. return false;
  1569. }
  1570. /* Any of these features mean the CMAX_WD field is valid. */
  1571. static bool mpam_has_cmax_wd_feature(struct mpam_props *props)
  1572. {
  1573. if (mpam_has_feature(mpam_feat_cmax_cmax, props))
  1574. return true;
  1575. if (mpam_has_feature(mpam_feat_cmax_cmin, props))
  1576. return true;
  1577. return false;
  1578. }
  1579. #define MISMATCHED_HELPER(parent, child, helper, field, alias) \
  1580. helper(parent) && \
  1581. ((helper(child) && (parent)->field != (child)->field) || \
  1582. (!helper(child) && !(alias)))
  1583. #define MISMATCHED_FEAT(parent, child, feat, field, alias) \
  1584. mpam_has_feature((feat), (parent)) && \
  1585. ((mpam_has_feature((feat), (child)) && (parent)->field != (child)->field) || \
  1586. (!mpam_has_feature((feat), (child)) && !(alias)))
  1587. #define CAN_MERGE_FEAT(parent, child, feat, alias) \
  1588. (alias) && !mpam_has_feature((feat), (parent)) && \
  1589. mpam_has_feature((feat), (child))
  1590. /*
  1591. * Combine two props fields.
  1592. * If this is for controls that alias the same resource, it is safe to just
  1593. * copy the values over. If two aliasing controls implement the same scheme
  1594. * a safe value must be picked.
  1595. * For non-aliasing controls, these control different resources, and the
  1596. * resulting safe value must be compatible with both. When merging values in
  1597. * the tree, all the aliasing resources must be handled first.
  1598. * On mismatch, parent is modified.
  1599. */
  1600. static void __props_mismatch(struct mpam_props *parent,
  1601. struct mpam_props *child, bool alias)
  1602. {
  1603. if (CAN_MERGE_FEAT(parent, child, mpam_feat_cpor_part, alias)) {
  1604. parent->cpbm_wd = child->cpbm_wd;
  1605. } else if (MISMATCHED_FEAT(parent, child, mpam_feat_cpor_part,
  1606. cpbm_wd, alias)) {
  1607. pr_debug("cleared cpor_part\n");
  1608. mpam_clear_feature(mpam_feat_cpor_part, parent);
  1609. parent->cpbm_wd = 0;
  1610. }
  1611. if (CAN_MERGE_FEAT(parent, child, mpam_feat_mbw_part, alias)) {
  1612. parent->mbw_pbm_bits = child->mbw_pbm_bits;
  1613. } else if (MISMATCHED_FEAT(parent, child, mpam_feat_mbw_part,
  1614. mbw_pbm_bits, alias)) {
  1615. pr_debug("cleared mbw_part\n");
  1616. mpam_clear_feature(mpam_feat_mbw_part, parent);
  1617. parent->mbw_pbm_bits = 0;
  1618. }
  1619. /* bwa_wd is a count of bits, fewer bits means less precision */
  1620. if (alias && !mpam_has_bwa_wd_feature(parent) &&
  1621. mpam_has_bwa_wd_feature(child)) {
  1622. parent->bwa_wd = child->bwa_wd;
  1623. } else if (MISMATCHED_HELPER(parent, child, mpam_has_bwa_wd_feature,
  1624. bwa_wd, alias)) {
  1625. pr_debug("took the min bwa_wd\n");
  1626. parent->bwa_wd = min(parent->bwa_wd, child->bwa_wd);
  1627. }
  1628. if (alias && !mpam_has_cmax_wd_feature(parent) && mpam_has_cmax_wd_feature(child)) {
  1629. parent->cmax_wd = child->cmax_wd;
  1630. } else if (MISMATCHED_HELPER(parent, child, mpam_has_cmax_wd_feature,
  1631. cmax_wd, alias)) {
  1632. pr_debug("%s took the min cmax_wd\n", __func__);
  1633. parent->cmax_wd = min(parent->cmax_wd, child->cmax_wd);
  1634. }
  1635. if (CAN_MERGE_FEAT(parent, child, mpam_feat_cmax_cassoc, alias)) {
  1636. parent->cassoc_wd = child->cassoc_wd;
  1637. } else if (MISMATCHED_FEAT(parent, child, mpam_feat_cmax_cassoc,
  1638. cassoc_wd, alias)) {
  1639. pr_debug("%s cleared cassoc_wd\n", __func__);
  1640. mpam_clear_feature(mpam_feat_cmax_cassoc, parent);
  1641. parent->cassoc_wd = 0;
  1642. }
  1643. /* For num properties, take the minimum */
  1644. if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_csu, alias)) {
  1645. parent->num_csu_mon = child->num_csu_mon;
  1646. } else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_csu,
  1647. num_csu_mon, alias)) {
  1648. pr_debug("took the min num_csu_mon\n");
  1649. parent->num_csu_mon = min(parent->num_csu_mon,
  1650. child->num_csu_mon);
  1651. }
  1652. if (CAN_MERGE_FEAT(parent, child, mpam_feat_msmon_mbwu, alias)) {
  1653. parent->num_mbwu_mon = child->num_mbwu_mon;
  1654. } else if (MISMATCHED_FEAT(parent, child, mpam_feat_msmon_mbwu,
  1655. num_mbwu_mon, alias)) {
  1656. pr_debug("took the min num_mbwu_mon\n");
  1657. parent->num_mbwu_mon = min(parent->num_mbwu_mon,
  1658. child->num_mbwu_mon);
  1659. }
  1660. if (CAN_MERGE_FEAT(parent, child, mpam_feat_intpri_part, alias)) {
  1661. parent->intpri_wd = child->intpri_wd;
  1662. } else if (MISMATCHED_FEAT(parent, child, mpam_feat_intpri_part,
  1663. intpri_wd, alias)) {
  1664. pr_debug("%s took the min intpri_wd\n", __func__);
  1665. parent->intpri_wd = min(parent->intpri_wd, child->intpri_wd);
  1666. }
  1667. if (CAN_MERGE_FEAT(parent, child, mpam_feat_dspri_part, alias)) {
  1668. parent->dspri_wd = child->dspri_wd;
  1669. } else if (MISMATCHED_FEAT(parent, child, mpam_feat_dspri_part,
  1670. dspri_wd, alias)) {
  1671. pr_debug("%s took the min dspri_wd\n", __func__);
  1672. parent->dspri_wd = min(parent->dspri_wd, child->dspri_wd);
  1673. }
  1674. /* TODO: alias support for these two */
  1675. /* {int,ds}pri may not have differing 0-low behaviour */
  1676. if (mpam_has_feature(mpam_feat_intpri_part, parent) &&
  1677. (!mpam_has_feature(mpam_feat_intpri_part, child) ||
  1678. mpam_has_feature(mpam_feat_intpri_part_0_low, parent) !=
  1679. mpam_has_feature(mpam_feat_intpri_part_0_low, child))) {
  1680. pr_debug("%s cleared intpri_part\n", __func__);
  1681. mpam_clear_feature(mpam_feat_intpri_part, parent);
  1682. mpam_clear_feature(mpam_feat_intpri_part_0_low, parent);
  1683. }
  1684. if (mpam_has_feature(mpam_feat_dspri_part, parent) &&
  1685. (!mpam_has_feature(mpam_feat_dspri_part, child) ||
  1686. mpam_has_feature(mpam_feat_dspri_part_0_low, parent) !=
  1687. mpam_has_feature(mpam_feat_dspri_part_0_low, child))) {
  1688. pr_debug("%s cleared dspri_part\n", __func__);
  1689. mpam_clear_feature(mpam_feat_dspri_part, parent);
  1690. mpam_clear_feature(mpam_feat_dspri_part_0_low, parent);
  1691. }
  1692. if (alias) {
  1693. /* Merge features for aliased resources */
  1694. bitmap_or(parent->features, parent->features, child->features, MPAM_FEATURE_LAST);
  1695. } else {
  1696. /* Clear missing features for non aliasing */
  1697. bitmap_and(parent->features, parent->features, child->features, MPAM_FEATURE_LAST);
  1698. }
  1699. }
  1700. /*
  1701. * If a vmsc doesn't match class feature/configuration, do the right thing(tm).
  1702. * For 'num' properties we can just take the minimum.
  1703. * For properties where the mismatched unused bits would make a difference, we
  1704. * nobble the class feature, as we can't configure all the resources.
  1705. * e.g. The L3 cache is composed of two resources with 13 and 17 portion
  1706. * bitmaps respectively.
  1707. */
  1708. static void
  1709. __class_props_mismatch(struct mpam_class *class, struct mpam_vmsc *vmsc)
  1710. {
  1711. struct mpam_props *cprops = &class->props;
  1712. struct mpam_props *vprops = &vmsc->props;
  1713. struct device *dev = &vmsc->msc->pdev->dev;
  1714. lockdep_assert_held(&mpam_list_lock); /* we modify class */
  1715. dev_dbg(dev, "Merging features for class:0x%lx &= vmsc:0x%lx\n",
  1716. (long)cprops->features, (long)vprops->features);
  1717. /* Take the safe value for any common features */
  1718. __props_mismatch(cprops, vprops, false);
  1719. }
  1720. static void
  1721. __vmsc_props_mismatch(struct mpam_vmsc *vmsc, struct mpam_msc_ris *ris)
  1722. {
  1723. struct mpam_props *rprops = &ris->props;
  1724. struct mpam_props *vprops = &vmsc->props;
  1725. struct device *dev = &vmsc->msc->pdev->dev;
  1726. lockdep_assert_held(&mpam_list_lock); /* we modify vmsc */
  1727. dev_dbg(dev, "Merging features for vmsc:0x%lx |= ris:0x%lx\n",
  1728. (long)vprops->features, (long)rprops->features);
  1729. /*
  1730. * Merge mismatched features - Copy any features that aren't common,
  1731. * but take the safe value for any common features.
  1732. */
  1733. __props_mismatch(vprops, rprops, true);
  1734. }
  1735. /*
  1736. * Copy the first component's first vMSC's properties and features to the
  1737. * class. __class_props_mismatch() will remove conflicts.
  1738. * It is not possible to have a class with no components, or a component with
  1739. * no resources. The vMSC properties have already been built.
  1740. */
  1741. static void mpam_enable_init_class_features(struct mpam_class *class)
  1742. {
  1743. struct mpam_vmsc *vmsc;
  1744. struct mpam_component *comp;
  1745. comp = list_first_entry(&class->components,
  1746. struct mpam_component, class_list);
  1747. vmsc = list_first_entry(&comp->vmsc,
  1748. struct mpam_vmsc, comp_list);
  1749. class->props = vmsc->props;
  1750. }
  1751. static void mpam_enable_merge_vmsc_features(struct mpam_component *comp)
  1752. {
  1753. struct mpam_vmsc *vmsc;
  1754. struct mpam_msc_ris *ris;
  1755. struct mpam_class *class = comp->class;
  1756. list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
  1757. list_for_each_entry(ris, &vmsc->ris, vmsc_list) {
  1758. __vmsc_props_mismatch(vmsc, ris);
  1759. class->nrdy_usec = max(class->nrdy_usec,
  1760. vmsc->msc->nrdy_usec);
  1761. }
  1762. }
  1763. }
  1764. static void mpam_enable_merge_class_features(struct mpam_component *comp)
  1765. {
  1766. struct mpam_vmsc *vmsc;
  1767. struct mpam_class *class = comp->class;
  1768. list_for_each_entry(vmsc, &comp->vmsc, comp_list)
  1769. __class_props_mismatch(class, vmsc);
  1770. }
  1771. /*
  1772. * Merge all the common resource features into class.
  1773. * vmsc features are bitwise-or'd together by mpam_enable_merge_vmsc_features()
  1774. * as the first step so that mpam_enable_init_class_features() can initialise
  1775. * the class with a representative set of features.
  1776. * Next the mpam_enable_merge_class_features() bitwise-and's all the vmsc
  1777. * features to form the class features.
  1778. * Other features are the min/max as appropriate.
  1779. *
  1780. * To avoid walking the whole tree twice, the class->nrdy_usec property is
  1781. * updated when working with the vmsc as it is a max(), and doesn't need
  1782. * initialising first.
  1783. */
  1784. static void mpam_enable_merge_features(struct list_head *all_classes_list)
  1785. {
  1786. struct mpam_class *class;
  1787. struct mpam_component *comp;
  1788. lockdep_assert_held(&mpam_list_lock);
  1789. list_for_each_entry(class, all_classes_list, classes_list) {
  1790. list_for_each_entry(comp, &class->components, class_list)
  1791. mpam_enable_merge_vmsc_features(comp);
  1792. mpam_enable_init_class_features(class);
  1793. list_for_each_entry(comp, &class->components, class_list)
  1794. mpam_enable_merge_class_features(comp);
  1795. }
  1796. }
  1797. static char *mpam_errcode_names[16] = {
  1798. [MPAM_ERRCODE_NONE] = "No error",
  1799. [MPAM_ERRCODE_PARTID_SEL_RANGE] = "PARTID_SEL_Range",
  1800. [MPAM_ERRCODE_REQ_PARTID_RANGE] = "Req_PARTID_Range",
  1801. [MPAM_ERRCODE_MSMONCFG_ID_RANGE] = "MSMONCFG_ID_RANGE",
  1802. [MPAM_ERRCODE_REQ_PMG_RANGE] = "Req_PMG_Range",
  1803. [MPAM_ERRCODE_MONITOR_RANGE] = "Monitor_Range",
  1804. [MPAM_ERRCODE_INTPARTID_RANGE] = "intPARTID_Range",
  1805. [MPAM_ERRCODE_UNEXPECTED_INTERNAL] = "Unexpected_INTERNAL",
  1806. [MPAM_ERRCODE_UNDEFINED_RIS_PART_SEL] = "Undefined_RIS_PART_SEL",
  1807. [MPAM_ERRCODE_RIS_NO_CONTROL] = "RIS_No_Control",
  1808. [MPAM_ERRCODE_UNDEFINED_RIS_MON_SEL] = "Undefined_RIS_MON_SEL",
  1809. [MPAM_ERRCODE_RIS_NO_MONITOR] = "RIS_No_Monitor",
  1810. [12 ... 15] = "Reserved"
  1811. };
  1812. static int mpam_enable_msc_ecr(void *_msc)
  1813. {
  1814. struct mpam_msc *msc = _msc;
  1815. __mpam_write_reg(msc, MPAMF_ECR, MPAMF_ECR_INTEN);
  1816. return 0;
  1817. }
  1818. /* This can run in mpam_disable(), and the interrupt handler on the same CPU */
  1819. static int mpam_disable_msc_ecr(void *_msc)
  1820. {
  1821. struct mpam_msc *msc = _msc;
  1822. __mpam_write_reg(msc, MPAMF_ECR, 0);
  1823. return 0;
  1824. }
  1825. static irqreturn_t __mpam_irq_handler(int irq, struct mpam_msc *msc)
  1826. {
  1827. u64 reg;
  1828. u16 partid;
  1829. u8 errcode, pmg, ris;
  1830. if (WARN_ON_ONCE(!msc) ||
  1831. WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(),
  1832. &msc->accessibility)))
  1833. return IRQ_NONE;
  1834. reg = mpam_msc_read_esr(msc);
  1835. errcode = FIELD_GET(MPAMF_ESR_ERRCODE, reg);
  1836. if (!errcode)
  1837. return IRQ_NONE;
  1838. /* Clear level triggered irq */
  1839. mpam_msc_clear_esr(msc);
  1840. partid = FIELD_GET(MPAMF_ESR_PARTID_MON, reg);
  1841. pmg = FIELD_GET(MPAMF_ESR_PMG, reg);
  1842. ris = FIELD_GET(MPAMF_ESR_RIS, reg);
  1843. pr_err_ratelimited("error irq from msc:%u '%s', partid:%u, pmg: %u, ris: %u\n",
  1844. msc->id, mpam_errcode_names[errcode], partid, pmg,
  1845. ris);
  1846. /* Disable this interrupt. */
  1847. mpam_disable_msc_ecr(msc);
  1848. /* Are we racing with the thread disabling MPAM? */
  1849. if (!mpam_is_enabled())
  1850. return IRQ_HANDLED;
  1851. /*
  1852. * Schedule the teardown work. Don't use a threaded IRQ as we can't
  1853. * unregister the interrupt from the threaded part of the handler.
  1854. */
  1855. mpam_disable_reason = "hardware error interrupt";
  1856. schedule_work(&mpam_broken_work);
  1857. return IRQ_HANDLED;
  1858. }
  1859. static irqreturn_t mpam_ppi_handler(int irq, void *dev_id)
  1860. {
  1861. struct mpam_msc *msc = *(struct mpam_msc **)dev_id;
  1862. return __mpam_irq_handler(irq, msc);
  1863. }
  1864. static irqreturn_t mpam_spi_handler(int irq, void *dev_id)
  1865. {
  1866. struct mpam_msc *msc = dev_id;
  1867. return __mpam_irq_handler(irq, msc);
  1868. }
  1869. static int mpam_register_irqs(void)
  1870. {
  1871. int err, irq;
  1872. struct mpam_msc *msc;
  1873. lockdep_assert_cpus_held();
  1874. guard(srcu)(&mpam_srcu);
  1875. list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
  1876. srcu_read_lock_held(&mpam_srcu)) {
  1877. irq = platform_get_irq_byname_optional(msc->pdev, "error");
  1878. if (irq <= 0)
  1879. continue;
  1880. /* The MPAM spec says the interrupt can be SPI, PPI or LPI */
  1881. /* We anticipate sharing the interrupt with other MSCs */
  1882. if (irq_is_percpu(irq)) {
  1883. err = request_percpu_irq(irq, &mpam_ppi_handler,
  1884. "mpam:msc:error",
  1885. msc->error_dev_id);
  1886. if (err)
  1887. return err;
  1888. msc->reenable_error_ppi = irq;
  1889. smp_call_function_many(&msc->accessibility,
  1890. &_enable_percpu_irq, &irq,
  1891. true);
  1892. } else {
  1893. err = devm_request_irq(&msc->pdev->dev, irq,
  1894. &mpam_spi_handler, IRQF_SHARED,
  1895. "mpam:msc:error", msc);
  1896. if (err)
  1897. return err;
  1898. }
  1899. mutex_lock(&msc->error_irq_lock);
  1900. msc->error_irq_req = true;
  1901. mpam_touch_msc(msc, mpam_enable_msc_ecr, msc);
  1902. msc->error_irq_hw_enabled = true;
  1903. mutex_unlock(&msc->error_irq_lock);
  1904. }
  1905. return 0;
  1906. }
  1907. static void mpam_unregister_irqs(void)
  1908. {
  1909. int irq;
  1910. struct mpam_msc *msc;
  1911. guard(cpus_read_lock)();
  1912. guard(srcu)(&mpam_srcu);
  1913. list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
  1914. srcu_read_lock_held(&mpam_srcu)) {
  1915. irq = platform_get_irq_byname_optional(msc->pdev, "error");
  1916. if (irq <= 0)
  1917. continue;
  1918. mutex_lock(&msc->error_irq_lock);
  1919. if (msc->error_irq_hw_enabled) {
  1920. mpam_touch_msc(msc, mpam_disable_msc_ecr, msc);
  1921. msc->error_irq_hw_enabled = false;
  1922. }
  1923. if (msc->error_irq_req) {
  1924. if (irq_is_percpu(irq)) {
  1925. msc->reenable_error_ppi = 0;
  1926. free_percpu_irq(irq, msc->error_dev_id);
  1927. } else {
  1928. devm_free_irq(&msc->pdev->dev, irq, msc);
  1929. }
  1930. msc->error_irq_req = false;
  1931. }
  1932. mutex_unlock(&msc->error_irq_lock);
  1933. }
  1934. }
  1935. static void __destroy_component_cfg(struct mpam_component *comp)
  1936. {
  1937. struct mpam_msc *msc;
  1938. struct mpam_vmsc *vmsc;
  1939. struct mpam_msc_ris *ris;
  1940. lockdep_assert_held(&mpam_list_lock);
  1941. add_to_garbage(comp->cfg);
  1942. list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
  1943. msc = vmsc->msc;
  1944. if (mpam_mon_sel_lock(msc)) {
  1945. list_for_each_entry(ris, &vmsc->ris, vmsc_list)
  1946. add_to_garbage(ris->mbwu_state);
  1947. mpam_mon_sel_unlock(msc);
  1948. }
  1949. }
  1950. }
  1951. static void mpam_reset_component_cfg(struct mpam_component *comp)
  1952. {
  1953. int i;
  1954. struct mpam_props *cprops = &comp->class->props;
  1955. mpam_assert_partid_sizes_fixed();
  1956. if (!comp->cfg)
  1957. return;
  1958. for (i = 0; i <= mpam_partid_max; i++) {
  1959. comp->cfg[i] = (struct mpam_config) {};
  1960. if (cprops->cpbm_wd)
  1961. comp->cfg[i].cpbm = GENMASK(cprops->cpbm_wd - 1, 0);
  1962. if (cprops->mbw_pbm_bits)
  1963. comp->cfg[i].mbw_pbm = GENMASK(cprops->mbw_pbm_bits - 1, 0);
  1964. if (cprops->bwa_wd)
  1965. comp->cfg[i].mbw_max = GENMASK(15, 16 - cprops->bwa_wd);
  1966. }
  1967. }
  1968. static int __allocate_component_cfg(struct mpam_component *comp)
  1969. {
  1970. struct mpam_vmsc *vmsc;
  1971. mpam_assert_partid_sizes_fixed();
  1972. if (comp->cfg)
  1973. return 0;
  1974. comp->cfg = kzalloc_objs(*comp->cfg, mpam_partid_max + 1);
  1975. if (!comp->cfg)
  1976. return -ENOMEM;
  1977. /*
  1978. * The array is free()d in one go, so only cfg[0]'s structure needs
  1979. * to be initialised.
  1980. */
  1981. init_garbage(&comp->cfg[0].garbage);
  1982. mpam_reset_component_cfg(comp);
  1983. list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
  1984. struct mpam_msc *msc;
  1985. struct mpam_msc_ris *ris;
  1986. struct msmon_mbwu_state *mbwu_state;
  1987. if (!vmsc->props.num_mbwu_mon)
  1988. continue;
  1989. msc = vmsc->msc;
  1990. list_for_each_entry(ris, &vmsc->ris, vmsc_list) {
  1991. if (!ris->props.num_mbwu_mon)
  1992. continue;
  1993. mbwu_state = kzalloc_objs(*ris->mbwu_state,
  1994. ris->props.num_mbwu_mon);
  1995. if (!mbwu_state) {
  1996. __destroy_component_cfg(comp);
  1997. return -ENOMEM;
  1998. }
  1999. init_garbage(&mbwu_state[0].garbage);
  2000. if (mpam_mon_sel_lock(msc)) {
  2001. ris->mbwu_state = mbwu_state;
  2002. mpam_mon_sel_unlock(msc);
  2003. }
  2004. }
  2005. }
  2006. return 0;
  2007. }
  2008. static int mpam_allocate_config(void)
  2009. {
  2010. struct mpam_class *class;
  2011. struct mpam_component *comp;
  2012. lockdep_assert_held(&mpam_list_lock);
  2013. list_for_each_entry(class, &mpam_classes, classes_list) {
  2014. list_for_each_entry(comp, &class->components, class_list) {
  2015. int err = __allocate_component_cfg(comp);
  2016. if (err)
  2017. return err;
  2018. }
  2019. }
  2020. return 0;
  2021. }
  2022. static void mpam_enable_once(void)
  2023. {
  2024. int err;
  2025. /*
  2026. * Once the cpuhp callbacks have been changed, mpam_partid_max can no
  2027. * longer change.
  2028. */
  2029. spin_lock(&partid_max_lock);
  2030. partid_max_published = true;
  2031. spin_unlock(&partid_max_lock);
  2032. /*
  2033. * If all the MSC have been probed, enabling the IRQs happens next.
  2034. * That involves cross-calling to a CPU that can reach the MSC, and
  2035. * the locks must be taken in this order:
  2036. */
  2037. cpus_read_lock();
  2038. mutex_lock(&mpam_list_lock);
  2039. do {
  2040. mpam_enable_merge_features(&mpam_classes);
  2041. err = mpam_register_irqs();
  2042. if (err) {
  2043. pr_warn("Failed to register irqs: %d\n", err);
  2044. break;
  2045. }
  2046. err = mpam_allocate_config();
  2047. if (err) {
  2048. pr_err("Failed to allocate configuration arrays.\n");
  2049. break;
  2050. }
  2051. } while (0);
  2052. mutex_unlock(&mpam_list_lock);
  2053. cpus_read_unlock();
  2054. if (err) {
  2055. mpam_disable_reason = "Failed to enable.";
  2056. schedule_work(&mpam_broken_work);
  2057. return;
  2058. }
  2059. static_branch_enable(&mpam_enabled);
  2060. mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline,
  2061. "mpam:online");
  2062. /* Use printk() to avoid the pr_fmt adding the function name. */
  2063. printk(KERN_INFO "MPAM enabled with %u PARTIDs and %u PMGs\n",
  2064. mpam_partid_max + 1, mpam_pmg_max + 1);
  2065. }
  2066. static void mpam_reset_component_locked(struct mpam_component *comp)
  2067. {
  2068. struct mpam_vmsc *vmsc;
  2069. lockdep_assert_cpus_held();
  2070. mpam_assert_partid_sizes_fixed();
  2071. mpam_reset_component_cfg(comp);
  2072. guard(srcu)(&mpam_srcu);
  2073. list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
  2074. srcu_read_lock_held(&mpam_srcu)) {
  2075. struct mpam_msc *msc = vmsc->msc;
  2076. struct mpam_msc_ris *ris;
  2077. list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
  2078. srcu_read_lock_held(&mpam_srcu)) {
  2079. if (!ris->in_reset_state)
  2080. mpam_touch_msc(msc, mpam_reset_ris, ris);
  2081. ris->in_reset_state = true;
  2082. }
  2083. }
  2084. }
  2085. static void mpam_reset_class_locked(struct mpam_class *class)
  2086. {
  2087. struct mpam_component *comp;
  2088. lockdep_assert_cpus_held();
  2089. guard(srcu)(&mpam_srcu);
  2090. list_for_each_entry_srcu(comp, &class->components, class_list,
  2091. srcu_read_lock_held(&mpam_srcu))
  2092. mpam_reset_component_locked(comp);
  2093. }
  2094. static void mpam_reset_class(struct mpam_class *class)
  2095. {
  2096. cpus_read_lock();
  2097. mpam_reset_class_locked(class);
  2098. cpus_read_unlock();
  2099. }
  2100. /*
  2101. * Called in response to an error IRQ.
  2102. * All of MPAMs errors indicate a software bug, restore any modified
  2103. * controls to their reset values.
  2104. */
  2105. void mpam_disable(struct work_struct *ignored)
  2106. {
  2107. int idx;
  2108. struct mpam_class *class;
  2109. struct mpam_msc *msc, *tmp;
  2110. mutex_lock(&mpam_cpuhp_state_lock);
  2111. if (mpam_cpuhp_state) {
  2112. cpuhp_remove_state(mpam_cpuhp_state);
  2113. mpam_cpuhp_state = 0;
  2114. }
  2115. mutex_unlock(&mpam_cpuhp_state_lock);
  2116. static_branch_disable(&mpam_enabled);
  2117. mpam_unregister_irqs();
  2118. idx = srcu_read_lock(&mpam_srcu);
  2119. list_for_each_entry_srcu(class, &mpam_classes, classes_list,
  2120. srcu_read_lock_held(&mpam_srcu))
  2121. mpam_reset_class(class);
  2122. srcu_read_unlock(&mpam_srcu, idx);
  2123. mutex_lock(&mpam_list_lock);
  2124. list_for_each_entry_safe(msc, tmp, &mpam_all_msc, all_msc_list)
  2125. mpam_msc_destroy(msc);
  2126. mutex_unlock(&mpam_list_lock);
  2127. mpam_free_garbage();
  2128. pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
  2129. }
  2130. /*
  2131. * Enable mpam once all devices have been probed.
  2132. * Scheduled by mpam_discovery_cpu_online() once all devices have been created.
  2133. * Also scheduled when new devices are probed when new CPUs come online.
  2134. */
  2135. void mpam_enable(struct work_struct *work)
  2136. {
  2137. static atomic_t once;
  2138. struct mpam_msc *msc;
  2139. bool all_devices_probed = true;
  2140. /* Have we probed all the hw devices? */
  2141. guard(srcu)(&mpam_srcu);
  2142. list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
  2143. srcu_read_lock_held(&mpam_srcu)) {
  2144. mutex_lock(&msc->probe_lock);
  2145. if (!msc->probed)
  2146. all_devices_probed = false;
  2147. mutex_unlock(&msc->probe_lock);
  2148. if (!all_devices_probed)
  2149. break;
  2150. }
  2151. if (all_devices_probed && !atomic_fetch_inc(&once))
  2152. mpam_enable_once();
  2153. }
  2154. #define maybe_update_config(cfg, feature, newcfg, member, changes) do { \
  2155. if (mpam_has_feature(feature, newcfg) && \
  2156. (newcfg)->member != (cfg)->member) { \
  2157. (cfg)->member = (newcfg)->member; \
  2158. mpam_set_feature(feature, cfg); \
  2159. \
  2160. (changes) = true; \
  2161. } \
  2162. } while (0)
  2163. static bool mpam_update_config(struct mpam_config *cfg,
  2164. const struct mpam_config *newcfg)
  2165. {
  2166. bool has_changes = false;
  2167. maybe_update_config(cfg, mpam_feat_cpor_part, newcfg, cpbm, has_changes);
  2168. maybe_update_config(cfg, mpam_feat_mbw_part, newcfg, mbw_pbm, has_changes);
  2169. maybe_update_config(cfg, mpam_feat_mbw_max, newcfg, mbw_max, has_changes);
  2170. return has_changes;
  2171. }
  2172. int mpam_apply_config(struct mpam_component *comp, u16 partid,
  2173. struct mpam_config *cfg)
  2174. {
  2175. struct mpam_write_config_arg arg;
  2176. struct mpam_msc_ris *ris;
  2177. struct mpam_vmsc *vmsc;
  2178. struct mpam_msc *msc;
  2179. lockdep_assert_cpus_held();
  2180. /* Don't pass in the current config! */
  2181. WARN_ON_ONCE(&comp->cfg[partid] == cfg);
  2182. if (!mpam_update_config(&comp->cfg[partid], cfg))
  2183. return 0;
  2184. arg.comp = comp;
  2185. arg.partid = partid;
  2186. guard(srcu)(&mpam_srcu);
  2187. list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
  2188. srcu_read_lock_held(&mpam_srcu)) {
  2189. msc = vmsc->msc;
  2190. mutex_lock(&msc->cfg_lock);
  2191. list_for_each_entry_srcu(ris, &vmsc->ris, vmsc_list,
  2192. srcu_read_lock_held(&mpam_srcu)) {
  2193. arg.ris = ris;
  2194. mpam_touch_msc(msc, __write_config, &arg);
  2195. }
  2196. mutex_unlock(&msc->cfg_lock);
  2197. }
  2198. return 0;
  2199. }
  2200. static int __init mpam_msc_driver_init(void)
  2201. {
  2202. if (!system_supports_mpam())
  2203. return -EOPNOTSUPP;
  2204. init_srcu_struct(&mpam_srcu);
  2205. fw_num_msc = acpi_mpam_count_msc();
  2206. if (fw_num_msc <= 0) {
  2207. pr_err("No MSC devices found in firmware\n");
  2208. return -EINVAL;
  2209. }
  2210. return platform_driver_register(&mpam_msc_driver);
  2211. }
  2212. /* Must occur after arm64_mpam_register_cpus() from arch_initcall() */
  2213. subsys_initcall(mpam_msc_driver_init);
  2214. #ifdef CONFIG_MPAM_KUNIT_TEST
  2215. #include "test_mpam_devices.c"
  2216. #endif