uncore.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. #include <linux/module.h>
  2. #include <asm/cpu_device_id.h>
  3. #include <asm/intel-family.h>
  4. #include "uncore.h"
  5. static struct intel_uncore_type *empty_uncore[] = { NULL, };
  6. struct intel_uncore_type **uncore_msr_uncores = empty_uncore;
  7. struct intel_uncore_type **uncore_pci_uncores = empty_uncore;
  8. static bool pcidrv_registered;
  9. struct pci_driver *uncore_pci_driver;
  10. /* pci bus to socket mapping */
  11. DEFINE_RAW_SPINLOCK(pci2phy_map_lock);
  12. struct list_head pci2phy_map_head = LIST_HEAD_INIT(pci2phy_map_head);
  13. struct pci_extra_dev *uncore_extra_pci_dev;
  14. static int max_packages;
  15. /* mask of cpus that collect uncore events */
  16. static cpumask_t uncore_cpu_mask;
  17. /* constraint for the fixed counter */
  18. static struct event_constraint uncore_constraint_fixed =
  19. EVENT_CONSTRAINT(~0ULL, 1 << UNCORE_PMC_IDX_FIXED, ~0ULL);
  20. struct event_constraint uncore_constraint_empty =
  21. EVENT_CONSTRAINT(0, 0, 0);
  22. MODULE_LICENSE("GPL");
  23. static int uncore_pcibus_to_physid(struct pci_bus *bus)
  24. {
  25. struct pci2phy_map *map;
  26. int phys_id = -1;
  27. raw_spin_lock(&pci2phy_map_lock);
  28. list_for_each_entry(map, &pci2phy_map_head, list) {
  29. if (map->segment == pci_domain_nr(bus)) {
  30. phys_id = map->pbus_to_physid[bus->number];
  31. break;
  32. }
  33. }
  34. raw_spin_unlock(&pci2phy_map_lock);
  35. return phys_id;
  36. }
  37. static void uncore_free_pcibus_map(void)
  38. {
  39. struct pci2phy_map *map, *tmp;
  40. list_for_each_entry_safe(map, tmp, &pci2phy_map_head, list) {
  41. list_del(&map->list);
  42. kfree(map);
  43. }
  44. }
  45. struct pci2phy_map *__find_pci2phy_map(int segment)
  46. {
  47. struct pci2phy_map *map, *alloc = NULL;
  48. int i;
  49. lockdep_assert_held(&pci2phy_map_lock);
  50. lookup:
  51. list_for_each_entry(map, &pci2phy_map_head, list) {
  52. if (map->segment == segment)
  53. goto end;
  54. }
  55. if (!alloc) {
  56. raw_spin_unlock(&pci2phy_map_lock);
  57. alloc = kmalloc(sizeof(struct pci2phy_map), GFP_KERNEL);
  58. raw_spin_lock(&pci2phy_map_lock);
  59. if (!alloc)
  60. return NULL;
  61. goto lookup;
  62. }
  63. map = alloc;
  64. alloc = NULL;
  65. map->segment = segment;
  66. for (i = 0; i < 256; i++)
  67. map->pbus_to_physid[i] = -1;
  68. list_add_tail(&map->list, &pci2phy_map_head);
  69. end:
  70. kfree(alloc);
  71. return map;
  72. }
  73. ssize_t uncore_event_show(struct kobject *kobj,
  74. struct kobj_attribute *attr, char *buf)
  75. {
  76. struct uncore_event_desc *event =
  77. container_of(attr, struct uncore_event_desc, attr);
  78. return sprintf(buf, "%s", event->config);
  79. }
  80. struct intel_uncore_box *uncore_pmu_to_box(struct intel_uncore_pmu *pmu, int cpu)
  81. {
  82. return pmu->boxes[topology_logical_package_id(cpu)];
  83. }
  84. u64 uncore_msr_read_counter(struct intel_uncore_box *box, struct perf_event *event)
  85. {
  86. u64 count;
  87. rdmsrl(event->hw.event_base, count);
  88. return count;
  89. }
  90. /*
  91. * generic get constraint function for shared match/mask registers.
  92. */
  93. struct event_constraint *
  94. uncore_get_constraint(struct intel_uncore_box *box, struct perf_event *event)
  95. {
  96. struct intel_uncore_extra_reg *er;
  97. struct hw_perf_event_extra *reg1 = &event->hw.extra_reg;
  98. struct hw_perf_event_extra *reg2 = &event->hw.branch_reg;
  99. unsigned long flags;
  100. bool ok = false;
  101. /*
  102. * reg->alloc can be set due to existing state, so for fake box we
  103. * need to ignore this, otherwise we might fail to allocate proper
  104. * fake state for this extra reg constraint.
  105. */
  106. if (reg1->idx == EXTRA_REG_NONE ||
  107. (!uncore_box_is_fake(box) && reg1->alloc))
  108. return NULL;
  109. er = &box->shared_regs[reg1->idx];
  110. raw_spin_lock_irqsave(&er->lock, flags);
  111. if (!atomic_read(&er->ref) ||
  112. (er->config1 == reg1->config && er->config2 == reg2->config)) {
  113. atomic_inc(&er->ref);
  114. er->config1 = reg1->config;
  115. er->config2 = reg2->config;
  116. ok = true;
  117. }
  118. raw_spin_unlock_irqrestore(&er->lock, flags);
  119. if (ok) {
  120. if (!uncore_box_is_fake(box))
  121. reg1->alloc = 1;
  122. return NULL;
  123. }
  124. return &uncore_constraint_empty;
  125. }
  126. void uncore_put_constraint(struct intel_uncore_box *box, struct perf_event *event)
  127. {
  128. struct intel_uncore_extra_reg *er;
  129. struct hw_perf_event_extra *reg1 = &event->hw.extra_reg;
  130. /*
  131. * Only put constraint if extra reg was actually allocated. Also
  132. * takes care of event which do not use an extra shared reg.
  133. *
  134. * Also, if this is a fake box we shouldn't touch any event state
  135. * (reg->alloc) and we don't care about leaving inconsistent box
  136. * state either since it will be thrown out.
  137. */
  138. if (uncore_box_is_fake(box) || !reg1->alloc)
  139. return;
  140. er = &box->shared_regs[reg1->idx];
  141. atomic_dec(&er->ref);
  142. reg1->alloc = 0;
  143. }
  144. u64 uncore_shared_reg_config(struct intel_uncore_box *box, int idx)
  145. {
  146. struct intel_uncore_extra_reg *er;
  147. unsigned long flags;
  148. u64 config;
  149. er = &box->shared_regs[idx];
  150. raw_spin_lock_irqsave(&er->lock, flags);
  151. config = er->config;
  152. raw_spin_unlock_irqrestore(&er->lock, flags);
  153. return config;
  154. }
  155. static void uncore_assign_hw_event(struct intel_uncore_box *box,
  156. struct perf_event *event, int idx)
  157. {
  158. struct hw_perf_event *hwc = &event->hw;
  159. hwc->idx = idx;
  160. hwc->last_tag = ++box->tags[idx];
  161. if (hwc->idx == UNCORE_PMC_IDX_FIXED) {
  162. hwc->event_base = uncore_fixed_ctr(box);
  163. hwc->config_base = uncore_fixed_ctl(box);
  164. return;
  165. }
  166. hwc->config_base = uncore_event_ctl(box, hwc->idx);
  167. hwc->event_base = uncore_perf_ctr(box, hwc->idx);
  168. }
  169. void uncore_perf_event_update(struct intel_uncore_box *box, struct perf_event *event)
  170. {
  171. u64 prev_count, new_count, delta;
  172. int shift;
  173. if (event->hw.idx >= UNCORE_PMC_IDX_FIXED)
  174. shift = 64 - uncore_fixed_ctr_bits(box);
  175. else
  176. shift = 64 - uncore_perf_ctr_bits(box);
  177. /* the hrtimer might modify the previous event value */
  178. again:
  179. prev_count = local64_read(&event->hw.prev_count);
  180. new_count = uncore_read_counter(box, event);
  181. if (local64_xchg(&event->hw.prev_count, new_count) != prev_count)
  182. goto again;
  183. delta = (new_count << shift) - (prev_count << shift);
  184. delta >>= shift;
  185. local64_add(delta, &event->count);
  186. }
  187. /*
  188. * The overflow interrupt is unavailable for SandyBridge-EP, is broken
  189. * for SandyBridge. So we use hrtimer to periodically poll the counter
  190. * to avoid overflow.
  191. */
  192. static enum hrtimer_restart uncore_pmu_hrtimer(struct hrtimer *hrtimer)
  193. {
  194. struct intel_uncore_box *box;
  195. struct perf_event *event;
  196. unsigned long flags;
  197. int bit;
  198. box = container_of(hrtimer, struct intel_uncore_box, hrtimer);
  199. if (!box->n_active || box->cpu != smp_processor_id())
  200. return HRTIMER_NORESTART;
  201. /*
  202. * disable local interrupt to prevent uncore_pmu_event_start/stop
  203. * to interrupt the update process
  204. */
  205. local_irq_save(flags);
  206. /*
  207. * handle boxes with an active event list as opposed to active
  208. * counters
  209. */
  210. list_for_each_entry(event, &box->active_list, active_entry) {
  211. uncore_perf_event_update(box, event);
  212. }
  213. for_each_set_bit(bit, box->active_mask, UNCORE_PMC_IDX_MAX)
  214. uncore_perf_event_update(box, box->events[bit]);
  215. local_irq_restore(flags);
  216. hrtimer_forward_now(hrtimer, ns_to_ktime(box->hrtimer_duration));
  217. return HRTIMER_RESTART;
  218. }
  219. void uncore_pmu_start_hrtimer(struct intel_uncore_box *box)
  220. {
  221. hrtimer_start(&box->hrtimer, ns_to_ktime(box->hrtimer_duration),
  222. HRTIMER_MODE_REL_PINNED);
  223. }
  224. void uncore_pmu_cancel_hrtimer(struct intel_uncore_box *box)
  225. {
  226. hrtimer_cancel(&box->hrtimer);
  227. }
  228. static void uncore_pmu_init_hrtimer(struct intel_uncore_box *box)
  229. {
  230. hrtimer_init(&box->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  231. box->hrtimer.function = uncore_pmu_hrtimer;
  232. }
  233. static struct intel_uncore_box *uncore_alloc_box(struct intel_uncore_type *type,
  234. int node)
  235. {
  236. int i, size, numshared = type->num_shared_regs ;
  237. struct intel_uncore_box *box;
  238. size = sizeof(*box) + numshared * sizeof(struct intel_uncore_extra_reg);
  239. box = kzalloc_node(size, GFP_KERNEL, node);
  240. if (!box)
  241. return NULL;
  242. for (i = 0; i < numshared; i++)
  243. raw_spin_lock_init(&box->shared_regs[i].lock);
  244. uncore_pmu_init_hrtimer(box);
  245. box->cpu = -1;
  246. box->pci_phys_id = -1;
  247. box->pkgid = -1;
  248. /* set default hrtimer timeout */
  249. box->hrtimer_duration = UNCORE_PMU_HRTIMER_INTERVAL;
  250. INIT_LIST_HEAD(&box->active_list);
  251. return box;
  252. }
  253. /*
  254. * Using uncore_pmu_event_init pmu event_init callback
  255. * as a detection point for uncore events.
  256. */
  257. static int uncore_pmu_event_init(struct perf_event *event);
  258. static bool is_box_event(struct intel_uncore_box *box, struct perf_event *event)
  259. {
  260. return &box->pmu->pmu == event->pmu;
  261. }
  262. static int
  263. uncore_collect_events(struct intel_uncore_box *box, struct perf_event *leader,
  264. bool dogrp)
  265. {
  266. struct perf_event *event;
  267. int n, max_count;
  268. max_count = box->pmu->type->num_counters;
  269. if (box->pmu->type->fixed_ctl)
  270. max_count++;
  271. if (box->n_events >= max_count)
  272. return -EINVAL;
  273. n = box->n_events;
  274. if (is_box_event(box, leader)) {
  275. box->event_list[n] = leader;
  276. n++;
  277. }
  278. if (!dogrp)
  279. return n;
  280. list_for_each_entry(event, &leader->sibling_list, group_entry) {
  281. if (!is_box_event(box, event) ||
  282. event->state <= PERF_EVENT_STATE_OFF)
  283. continue;
  284. if (n >= max_count)
  285. return -EINVAL;
  286. box->event_list[n] = event;
  287. n++;
  288. }
  289. return n;
  290. }
  291. static struct event_constraint *
  292. uncore_get_event_constraint(struct intel_uncore_box *box, struct perf_event *event)
  293. {
  294. struct intel_uncore_type *type = box->pmu->type;
  295. struct event_constraint *c;
  296. if (type->ops->get_constraint) {
  297. c = type->ops->get_constraint(box, event);
  298. if (c)
  299. return c;
  300. }
  301. if (event->attr.config == UNCORE_FIXED_EVENT)
  302. return &uncore_constraint_fixed;
  303. if (type->constraints) {
  304. for_each_event_constraint(c, type->constraints) {
  305. if ((event->hw.config & c->cmask) == c->code)
  306. return c;
  307. }
  308. }
  309. return &type->unconstrainted;
  310. }
  311. static void uncore_put_event_constraint(struct intel_uncore_box *box,
  312. struct perf_event *event)
  313. {
  314. if (box->pmu->type->ops->put_constraint)
  315. box->pmu->type->ops->put_constraint(box, event);
  316. }
  317. static int uncore_assign_events(struct intel_uncore_box *box, int assign[], int n)
  318. {
  319. unsigned long used_mask[BITS_TO_LONGS(UNCORE_PMC_IDX_MAX)];
  320. struct event_constraint *c;
  321. int i, wmin, wmax, ret = 0;
  322. struct hw_perf_event *hwc;
  323. bitmap_zero(used_mask, UNCORE_PMC_IDX_MAX);
  324. for (i = 0, wmin = UNCORE_PMC_IDX_MAX, wmax = 0; i < n; i++) {
  325. c = uncore_get_event_constraint(box, box->event_list[i]);
  326. box->event_constraint[i] = c;
  327. wmin = min(wmin, c->weight);
  328. wmax = max(wmax, c->weight);
  329. }
  330. /* fastpath, try to reuse previous register */
  331. for (i = 0; i < n; i++) {
  332. hwc = &box->event_list[i]->hw;
  333. c = box->event_constraint[i];
  334. /* never assigned */
  335. if (hwc->idx == -1)
  336. break;
  337. /* constraint still honored */
  338. if (!test_bit(hwc->idx, c->idxmsk))
  339. break;
  340. /* not already used */
  341. if (test_bit(hwc->idx, used_mask))
  342. break;
  343. __set_bit(hwc->idx, used_mask);
  344. if (assign)
  345. assign[i] = hwc->idx;
  346. }
  347. /* slow path */
  348. if (i != n)
  349. ret = perf_assign_events(box->event_constraint, n,
  350. wmin, wmax, n, assign);
  351. if (!assign || ret) {
  352. for (i = 0; i < n; i++)
  353. uncore_put_event_constraint(box, box->event_list[i]);
  354. }
  355. return ret ? -EINVAL : 0;
  356. }
  357. static void uncore_pmu_event_start(struct perf_event *event, int flags)
  358. {
  359. struct intel_uncore_box *box = uncore_event_to_box(event);
  360. int idx = event->hw.idx;
  361. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  362. return;
  363. if (WARN_ON_ONCE(idx == -1 || idx >= UNCORE_PMC_IDX_MAX))
  364. return;
  365. event->hw.state = 0;
  366. box->events[idx] = event;
  367. box->n_active++;
  368. __set_bit(idx, box->active_mask);
  369. local64_set(&event->hw.prev_count, uncore_read_counter(box, event));
  370. uncore_enable_event(box, event);
  371. if (box->n_active == 1) {
  372. uncore_enable_box(box);
  373. uncore_pmu_start_hrtimer(box);
  374. }
  375. }
  376. static void uncore_pmu_event_stop(struct perf_event *event, int flags)
  377. {
  378. struct intel_uncore_box *box = uncore_event_to_box(event);
  379. struct hw_perf_event *hwc = &event->hw;
  380. if (__test_and_clear_bit(hwc->idx, box->active_mask)) {
  381. uncore_disable_event(box, event);
  382. box->n_active--;
  383. box->events[hwc->idx] = NULL;
  384. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  385. hwc->state |= PERF_HES_STOPPED;
  386. if (box->n_active == 0) {
  387. uncore_disable_box(box);
  388. uncore_pmu_cancel_hrtimer(box);
  389. }
  390. }
  391. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  392. /*
  393. * Drain the remaining delta count out of a event
  394. * that we are disabling:
  395. */
  396. uncore_perf_event_update(box, event);
  397. hwc->state |= PERF_HES_UPTODATE;
  398. }
  399. }
  400. static int uncore_pmu_event_add(struct perf_event *event, int flags)
  401. {
  402. struct intel_uncore_box *box = uncore_event_to_box(event);
  403. struct hw_perf_event *hwc = &event->hw;
  404. int assign[UNCORE_PMC_IDX_MAX];
  405. int i, n, ret;
  406. if (!box)
  407. return -ENODEV;
  408. ret = n = uncore_collect_events(box, event, false);
  409. if (ret < 0)
  410. return ret;
  411. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  412. if (!(flags & PERF_EF_START))
  413. hwc->state |= PERF_HES_ARCH;
  414. ret = uncore_assign_events(box, assign, n);
  415. if (ret)
  416. return ret;
  417. /* save events moving to new counters */
  418. for (i = 0; i < box->n_events; i++) {
  419. event = box->event_list[i];
  420. hwc = &event->hw;
  421. if (hwc->idx == assign[i] &&
  422. hwc->last_tag == box->tags[assign[i]])
  423. continue;
  424. /*
  425. * Ensure we don't accidentally enable a stopped
  426. * counter simply because we rescheduled.
  427. */
  428. if (hwc->state & PERF_HES_STOPPED)
  429. hwc->state |= PERF_HES_ARCH;
  430. uncore_pmu_event_stop(event, PERF_EF_UPDATE);
  431. }
  432. /* reprogram moved events into new counters */
  433. for (i = 0; i < n; i++) {
  434. event = box->event_list[i];
  435. hwc = &event->hw;
  436. if (hwc->idx != assign[i] ||
  437. hwc->last_tag != box->tags[assign[i]])
  438. uncore_assign_hw_event(box, event, assign[i]);
  439. else if (i < box->n_events)
  440. continue;
  441. if (hwc->state & PERF_HES_ARCH)
  442. continue;
  443. uncore_pmu_event_start(event, 0);
  444. }
  445. box->n_events = n;
  446. return 0;
  447. }
  448. static void uncore_pmu_event_del(struct perf_event *event, int flags)
  449. {
  450. struct intel_uncore_box *box = uncore_event_to_box(event);
  451. int i;
  452. uncore_pmu_event_stop(event, PERF_EF_UPDATE);
  453. for (i = 0; i < box->n_events; i++) {
  454. if (event == box->event_list[i]) {
  455. uncore_put_event_constraint(box, event);
  456. for (++i; i < box->n_events; i++)
  457. box->event_list[i - 1] = box->event_list[i];
  458. --box->n_events;
  459. break;
  460. }
  461. }
  462. event->hw.idx = -1;
  463. event->hw.last_tag = ~0ULL;
  464. }
  465. void uncore_pmu_event_read(struct perf_event *event)
  466. {
  467. struct intel_uncore_box *box = uncore_event_to_box(event);
  468. uncore_perf_event_update(box, event);
  469. }
  470. /*
  471. * validation ensures the group can be loaded onto the
  472. * PMU if it was the only group available.
  473. */
  474. static int uncore_validate_group(struct intel_uncore_pmu *pmu,
  475. struct perf_event *event)
  476. {
  477. struct perf_event *leader = event->group_leader;
  478. struct intel_uncore_box *fake_box;
  479. int ret = -EINVAL, n;
  480. fake_box = uncore_alloc_box(pmu->type, NUMA_NO_NODE);
  481. if (!fake_box)
  482. return -ENOMEM;
  483. fake_box->pmu = pmu;
  484. /*
  485. * the event is not yet connected with its
  486. * siblings therefore we must first collect
  487. * existing siblings, then add the new event
  488. * before we can simulate the scheduling
  489. */
  490. n = uncore_collect_events(fake_box, leader, true);
  491. if (n < 0)
  492. goto out;
  493. fake_box->n_events = n;
  494. n = uncore_collect_events(fake_box, event, false);
  495. if (n < 0)
  496. goto out;
  497. fake_box->n_events = n;
  498. ret = uncore_assign_events(fake_box, NULL, n);
  499. out:
  500. kfree(fake_box);
  501. return ret;
  502. }
  503. static int uncore_pmu_event_init(struct perf_event *event)
  504. {
  505. struct intel_uncore_pmu *pmu;
  506. struct intel_uncore_box *box;
  507. struct hw_perf_event *hwc = &event->hw;
  508. int ret;
  509. if (event->attr.type != event->pmu->type)
  510. return -ENOENT;
  511. pmu = uncore_event_to_pmu(event);
  512. /* no device found for this pmu */
  513. if (pmu->func_id < 0)
  514. return -ENOENT;
  515. /*
  516. * Uncore PMU does measure at all privilege level all the time.
  517. * So it doesn't make sense to specify any exclude bits.
  518. */
  519. if (event->attr.exclude_user || event->attr.exclude_kernel ||
  520. event->attr.exclude_hv || event->attr.exclude_idle)
  521. return -EINVAL;
  522. /* Sampling not supported yet */
  523. if (hwc->sample_period)
  524. return -EINVAL;
  525. /*
  526. * Place all uncore events for a particular physical package
  527. * onto a single cpu
  528. */
  529. if (event->cpu < 0)
  530. return -EINVAL;
  531. box = uncore_pmu_to_box(pmu, event->cpu);
  532. if (!box || box->cpu < 0)
  533. return -EINVAL;
  534. event->cpu = box->cpu;
  535. event->pmu_private = box;
  536. event->event_caps |= PERF_EV_CAP_READ_ACTIVE_PKG;
  537. event->hw.idx = -1;
  538. event->hw.last_tag = ~0ULL;
  539. event->hw.extra_reg.idx = EXTRA_REG_NONE;
  540. event->hw.branch_reg.idx = EXTRA_REG_NONE;
  541. if (event->attr.config == UNCORE_FIXED_EVENT) {
  542. /* no fixed counter */
  543. if (!pmu->type->fixed_ctl)
  544. return -EINVAL;
  545. /*
  546. * if there is only one fixed counter, only the first pmu
  547. * can access the fixed counter
  548. */
  549. if (pmu->type->single_fixed && pmu->pmu_idx > 0)
  550. return -EINVAL;
  551. /* fixed counters have event field hardcoded to zero */
  552. hwc->config = 0ULL;
  553. } else {
  554. hwc->config = event->attr.config &
  555. (pmu->type->event_mask | ((u64)pmu->type->event_mask_ext << 32));
  556. if (pmu->type->ops->hw_config) {
  557. ret = pmu->type->ops->hw_config(box, event);
  558. if (ret)
  559. return ret;
  560. }
  561. }
  562. if (event->group_leader != event)
  563. ret = uncore_validate_group(pmu, event);
  564. else
  565. ret = 0;
  566. return ret;
  567. }
  568. static ssize_t uncore_get_attr_cpumask(struct device *dev,
  569. struct device_attribute *attr, char *buf)
  570. {
  571. return cpumap_print_to_pagebuf(true, buf, &uncore_cpu_mask);
  572. }
  573. static DEVICE_ATTR(cpumask, S_IRUGO, uncore_get_attr_cpumask, NULL);
  574. static struct attribute *uncore_pmu_attrs[] = {
  575. &dev_attr_cpumask.attr,
  576. NULL,
  577. };
  578. static struct attribute_group uncore_pmu_attr_group = {
  579. .attrs = uncore_pmu_attrs,
  580. };
  581. static int uncore_pmu_register(struct intel_uncore_pmu *pmu)
  582. {
  583. int ret;
  584. if (!pmu->type->pmu) {
  585. pmu->pmu = (struct pmu) {
  586. .attr_groups = pmu->type->attr_groups,
  587. .task_ctx_nr = perf_invalid_context,
  588. .event_init = uncore_pmu_event_init,
  589. .add = uncore_pmu_event_add,
  590. .del = uncore_pmu_event_del,
  591. .start = uncore_pmu_event_start,
  592. .stop = uncore_pmu_event_stop,
  593. .read = uncore_pmu_event_read,
  594. .module = THIS_MODULE,
  595. };
  596. } else {
  597. pmu->pmu = *pmu->type->pmu;
  598. pmu->pmu.attr_groups = pmu->type->attr_groups;
  599. }
  600. if (pmu->type->num_boxes == 1) {
  601. if (strlen(pmu->type->name) > 0)
  602. sprintf(pmu->name, "uncore_%s", pmu->type->name);
  603. else
  604. sprintf(pmu->name, "uncore");
  605. } else {
  606. sprintf(pmu->name, "uncore_%s_%d", pmu->type->name,
  607. pmu->pmu_idx);
  608. }
  609. ret = perf_pmu_register(&pmu->pmu, pmu->name, -1);
  610. if (!ret)
  611. pmu->registered = true;
  612. return ret;
  613. }
  614. static void uncore_pmu_unregister(struct intel_uncore_pmu *pmu)
  615. {
  616. if (!pmu->registered)
  617. return;
  618. perf_pmu_unregister(&pmu->pmu);
  619. pmu->registered = false;
  620. }
  621. static void __uncore_exit_boxes(struct intel_uncore_type *type, int cpu)
  622. {
  623. struct intel_uncore_pmu *pmu = type->pmus;
  624. struct intel_uncore_box *box;
  625. int i, pkg;
  626. if (pmu) {
  627. pkg = topology_physical_package_id(cpu);
  628. for (i = 0; i < type->num_boxes; i++, pmu++) {
  629. box = pmu->boxes[pkg];
  630. if (box)
  631. uncore_box_exit(box);
  632. }
  633. }
  634. }
  635. static void uncore_exit_boxes(void *dummy)
  636. {
  637. struct intel_uncore_type **types;
  638. for (types = uncore_msr_uncores; *types; types++)
  639. __uncore_exit_boxes(*types++, smp_processor_id());
  640. }
  641. static void uncore_free_boxes(struct intel_uncore_pmu *pmu)
  642. {
  643. int pkg;
  644. for (pkg = 0; pkg < max_packages; pkg++)
  645. kfree(pmu->boxes[pkg]);
  646. kfree(pmu->boxes);
  647. }
  648. static void uncore_type_exit(struct intel_uncore_type *type)
  649. {
  650. struct intel_uncore_pmu *pmu = type->pmus;
  651. int i;
  652. if (pmu) {
  653. for (i = 0; i < type->num_boxes; i++, pmu++) {
  654. uncore_pmu_unregister(pmu);
  655. uncore_free_boxes(pmu);
  656. }
  657. kfree(type->pmus);
  658. type->pmus = NULL;
  659. }
  660. kfree(type->events_group);
  661. type->events_group = NULL;
  662. }
  663. static void uncore_types_exit(struct intel_uncore_type **types)
  664. {
  665. for (; *types; types++)
  666. uncore_type_exit(*types);
  667. }
  668. static int __init uncore_type_init(struct intel_uncore_type *type, bool setid)
  669. {
  670. struct intel_uncore_pmu *pmus;
  671. struct attribute_group *attr_group;
  672. struct attribute **attrs;
  673. size_t size;
  674. int i, j;
  675. pmus = kzalloc(sizeof(*pmus) * type->num_boxes, GFP_KERNEL);
  676. if (!pmus)
  677. return -ENOMEM;
  678. size = max_packages * sizeof(struct intel_uncore_box *);
  679. for (i = 0; i < type->num_boxes; i++) {
  680. pmus[i].func_id = setid ? i : -1;
  681. pmus[i].pmu_idx = i;
  682. pmus[i].type = type;
  683. pmus[i].boxes = kzalloc(size, GFP_KERNEL);
  684. if (!pmus[i].boxes)
  685. return -ENOMEM;
  686. }
  687. type->pmus = pmus;
  688. type->unconstrainted = (struct event_constraint)
  689. __EVENT_CONSTRAINT(0, (1ULL << type->num_counters) - 1,
  690. 0, type->num_counters, 0, 0);
  691. if (type->event_descs) {
  692. for (i = 0; type->event_descs[i].attr.attr.name; i++);
  693. attr_group = kzalloc(sizeof(struct attribute *) * (i + 1) +
  694. sizeof(*attr_group), GFP_KERNEL);
  695. if (!attr_group)
  696. return -ENOMEM;
  697. attrs = (struct attribute **)(attr_group + 1);
  698. attr_group->name = "events";
  699. attr_group->attrs = attrs;
  700. for (j = 0; j < i; j++)
  701. attrs[j] = &type->event_descs[j].attr.attr;
  702. type->events_group = attr_group;
  703. }
  704. type->pmu_group = &uncore_pmu_attr_group;
  705. return 0;
  706. }
  707. static int __init
  708. uncore_types_init(struct intel_uncore_type **types, bool setid)
  709. {
  710. int ret;
  711. for (; *types; types++) {
  712. ret = uncore_type_init(*types, setid);
  713. if (ret)
  714. return ret;
  715. }
  716. return 0;
  717. }
  718. /*
  719. * add a pci uncore device
  720. */
  721. static int uncore_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  722. {
  723. struct intel_uncore_type *type;
  724. struct intel_uncore_pmu *pmu = NULL;
  725. struct intel_uncore_box *box;
  726. int phys_id, pkg, ret;
  727. phys_id = uncore_pcibus_to_physid(pdev->bus);
  728. if (phys_id < 0)
  729. return -ENODEV;
  730. pkg = topology_phys_to_logical_pkg(phys_id);
  731. if (pkg < 0)
  732. return -EINVAL;
  733. if (UNCORE_PCI_DEV_TYPE(id->driver_data) == UNCORE_EXTRA_PCI_DEV) {
  734. int idx = UNCORE_PCI_DEV_IDX(id->driver_data);
  735. uncore_extra_pci_dev[pkg].dev[idx] = pdev;
  736. pci_set_drvdata(pdev, NULL);
  737. return 0;
  738. }
  739. type = uncore_pci_uncores[UNCORE_PCI_DEV_TYPE(id->driver_data)];
  740. /*
  741. * Some platforms, e.g. Knights Landing, use a common PCI device ID
  742. * for multiple instances of an uncore PMU device type. We should check
  743. * PCI slot and func to indicate the uncore box.
  744. */
  745. if (id->driver_data & ~0xffff) {
  746. struct pci_driver *pci_drv = pdev->driver;
  747. const struct pci_device_id *ids = pci_drv->id_table;
  748. unsigned int devfn;
  749. while (ids && ids->vendor) {
  750. if ((ids->vendor == pdev->vendor) &&
  751. (ids->device == pdev->device)) {
  752. devfn = PCI_DEVFN(UNCORE_PCI_DEV_DEV(ids->driver_data),
  753. UNCORE_PCI_DEV_FUNC(ids->driver_data));
  754. if (devfn == pdev->devfn) {
  755. pmu = &type->pmus[UNCORE_PCI_DEV_IDX(ids->driver_data)];
  756. break;
  757. }
  758. }
  759. ids++;
  760. }
  761. if (pmu == NULL)
  762. return -ENODEV;
  763. } else {
  764. /*
  765. * for performance monitoring unit with multiple boxes,
  766. * each box has a different function id.
  767. */
  768. pmu = &type->pmus[UNCORE_PCI_DEV_IDX(id->driver_data)];
  769. }
  770. if (WARN_ON_ONCE(pmu->boxes[pkg] != NULL))
  771. return -EINVAL;
  772. box = uncore_alloc_box(type, NUMA_NO_NODE);
  773. if (!box)
  774. return -ENOMEM;
  775. if (pmu->func_id < 0)
  776. pmu->func_id = pdev->devfn;
  777. else
  778. WARN_ON_ONCE(pmu->func_id != pdev->devfn);
  779. atomic_inc(&box->refcnt);
  780. box->pci_phys_id = phys_id;
  781. box->pkgid = pkg;
  782. box->pci_dev = pdev;
  783. box->pmu = pmu;
  784. uncore_box_init(box);
  785. pci_set_drvdata(pdev, box);
  786. pmu->boxes[pkg] = box;
  787. if (atomic_inc_return(&pmu->activeboxes) > 1)
  788. return 0;
  789. /* First active box registers the pmu */
  790. ret = uncore_pmu_register(pmu);
  791. if (ret) {
  792. pci_set_drvdata(pdev, NULL);
  793. pmu->boxes[pkg] = NULL;
  794. uncore_box_exit(box);
  795. kfree(box);
  796. }
  797. return ret;
  798. }
  799. static void uncore_pci_remove(struct pci_dev *pdev)
  800. {
  801. struct intel_uncore_box *box;
  802. struct intel_uncore_pmu *pmu;
  803. int i, phys_id, pkg;
  804. phys_id = uncore_pcibus_to_physid(pdev->bus);
  805. pkg = topology_phys_to_logical_pkg(phys_id);
  806. box = pci_get_drvdata(pdev);
  807. if (!box) {
  808. for (i = 0; i < UNCORE_EXTRA_PCI_DEV_MAX; i++) {
  809. if (uncore_extra_pci_dev[pkg].dev[i] == pdev) {
  810. uncore_extra_pci_dev[pkg].dev[i] = NULL;
  811. break;
  812. }
  813. }
  814. WARN_ON_ONCE(i >= UNCORE_EXTRA_PCI_DEV_MAX);
  815. return;
  816. }
  817. pmu = box->pmu;
  818. if (WARN_ON_ONCE(phys_id != box->pci_phys_id))
  819. return;
  820. pci_set_drvdata(pdev, NULL);
  821. pmu->boxes[pkg] = NULL;
  822. if (atomic_dec_return(&pmu->activeboxes) == 0)
  823. uncore_pmu_unregister(pmu);
  824. uncore_box_exit(box);
  825. kfree(box);
  826. }
  827. static int __init uncore_pci_init(void)
  828. {
  829. size_t size;
  830. int ret;
  831. size = max_packages * sizeof(struct pci_extra_dev);
  832. uncore_extra_pci_dev = kzalloc(size, GFP_KERNEL);
  833. if (!uncore_extra_pci_dev) {
  834. ret = -ENOMEM;
  835. goto err;
  836. }
  837. ret = uncore_types_init(uncore_pci_uncores, false);
  838. if (ret)
  839. goto errtype;
  840. uncore_pci_driver->probe = uncore_pci_probe;
  841. uncore_pci_driver->remove = uncore_pci_remove;
  842. ret = pci_register_driver(uncore_pci_driver);
  843. if (ret)
  844. goto errtype;
  845. pcidrv_registered = true;
  846. return 0;
  847. errtype:
  848. uncore_types_exit(uncore_pci_uncores);
  849. kfree(uncore_extra_pci_dev);
  850. uncore_extra_pci_dev = NULL;
  851. uncore_free_pcibus_map();
  852. err:
  853. uncore_pci_uncores = empty_uncore;
  854. return ret;
  855. }
  856. static void uncore_pci_exit(void)
  857. {
  858. if (pcidrv_registered) {
  859. pcidrv_registered = false;
  860. pci_unregister_driver(uncore_pci_driver);
  861. uncore_types_exit(uncore_pci_uncores);
  862. kfree(uncore_extra_pci_dev);
  863. uncore_free_pcibus_map();
  864. }
  865. }
  866. static int uncore_cpu_dying(unsigned int cpu)
  867. {
  868. struct intel_uncore_type *type, **types = uncore_msr_uncores;
  869. struct intel_uncore_pmu *pmu;
  870. struct intel_uncore_box *box;
  871. int i, pkg;
  872. pkg = topology_logical_package_id(cpu);
  873. for (; *types; types++) {
  874. type = *types;
  875. pmu = type->pmus;
  876. for (i = 0; i < type->num_boxes; i++, pmu++) {
  877. box = pmu->boxes[pkg];
  878. if (box && atomic_dec_return(&box->refcnt) == 0)
  879. uncore_box_exit(box);
  880. }
  881. }
  882. return 0;
  883. }
  884. static int first_init;
  885. static int uncore_cpu_starting(unsigned int cpu)
  886. {
  887. struct intel_uncore_type *type, **types = uncore_msr_uncores;
  888. struct intel_uncore_pmu *pmu;
  889. struct intel_uncore_box *box;
  890. int i, pkg, ncpus = 1;
  891. if (first_init) {
  892. /*
  893. * On init we get the number of online cpus in the package
  894. * and set refcount for all of them.
  895. */
  896. ncpus = cpumask_weight(topology_core_cpumask(cpu));
  897. }
  898. pkg = topology_logical_package_id(cpu);
  899. for (; *types; types++) {
  900. type = *types;
  901. pmu = type->pmus;
  902. for (i = 0; i < type->num_boxes; i++, pmu++) {
  903. box = pmu->boxes[pkg];
  904. if (!box)
  905. continue;
  906. /* The first cpu on a package activates the box */
  907. if (atomic_add_return(ncpus, &box->refcnt) == ncpus)
  908. uncore_box_init(box);
  909. }
  910. }
  911. return 0;
  912. }
  913. static int uncore_cpu_prepare(unsigned int cpu)
  914. {
  915. struct intel_uncore_type *type, **types = uncore_msr_uncores;
  916. struct intel_uncore_pmu *pmu;
  917. struct intel_uncore_box *box;
  918. int i, pkg;
  919. pkg = topology_logical_package_id(cpu);
  920. for (; *types; types++) {
  921. type = *types;
  922. pmu = type->pmus;
  923. for (i = 0; i < type->num_boxes; i++, pmu++) {
  924. if (pmu->boxes[pkg])
  925. continue;
  926. /* First cpu of a package allocates the box */
  927. box = uncore_alloc_box(type, cpu_to_node(cpu));
  928. if (!box)
  929. return -ENOMEM;
  930. box->pmu = pmu;
  931. box->pkgid = pkg;
  932. pmu->boxes[pkg] = box;
  933. }
  934. }
  935. return 0;
  936. }
  937. static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu,
  938. int new_cpu)
  939. {
  940. struct intel_uncore_pmu *pmu = type->pmus;
  941. struct intel_uncore_box *box;
  942. int i, pkg;
  943. pkg = topology_logical_package_id(old_cpu < 0 ? new_cpu : old_cpu);
  944. for (i = 0; i < type->num_boxes; i++, pmu++) {
  945. box = pmu->boxes[pkg];
  946. if (!box)
  947. continue;
  948. if (old_cpu < 0) {
  949. WARN_ON_ONCE(box->cpu != -1);
  950. box->cpu = new_cpu;
  951. continue;
  952. }
  953. WARN_ON_ONCE(box->cpu != old_cpu);
  954. box->cpu = -1;
  955. if (new_cpu < 0)
  956. continue;
  957. uncore_pmu_cancel_hrtimer(box);
  958. perf_pmu_migrate_context(&pmu->pmu, old_cpu, new_cpu);
  959. box->cpu = new_cpu;
  960. }
  961. }
  962. static void uncore_change_context(struct intel_uncore_type **uncores,
  963. int old_cpu, int new_cpu)
  964. {
  965. for (; *uncores; uncores++)
  966. uncore_change_type_ctx(*uncores, old_cpu, new_cpu);
  967. }
  968. static int uncore_event_cpu_offline(unsigned int cpu)
  969. {
  970. int target;
  971. /* Check if exiting cpu is used for collecting uncore events */
  972. if (!cpumask_test_and_clear_cpu(cpu, &uncore_cpu_mask))
  973. return 0;
  974. /* Find a new cpu to collect uncore events */
  975. target = cpumask_any_but(topology_core_cpumask(cpu), cpu);
  976. /* Migrate uncore events to the new target */
  977. if (target < nr_cpu_ids)
  978. cpumask_set_cpu(target, &uncore_cpu_mask);
  979. else
  980. target = -1;
  981. uncore_change_context(uncore_msr_uncores, cpu, target);
  982. uncore_change_context(uncore_pci_uncores, cpu, target);
  983. return 0;
  984. }
  985. static int uncore_event_cpu_online(unsigned int cpu)
  986. {
  987. int target;
  988. /*
  989. * Check if there is an online cpu in the package
  990. * which collects uncore events already.
  991. */
  992. target = cpumask_any_and(&uncore_cpu_mask, topology_core_cpumask(cpu));
  993. if (target < nr_cpu_ids)
  994. return 0;
  995. cpumask_set_cpu(cpu, &uncore_cpu_mask);
  996. uncore_change_context(uncore_msr_uncores, -1, cpu);
  997. uncore_change_context(uncore_pci_uncores, -1, cpu);
  998. return 0;
  999. }
  1000. static int __init type_pmu_register(struct intel_uncore_type *type)
  1001. {
  1002. int i, ret;
  1003. for (i = 0; i < type->num_boxes; i++) {
  1004. ret = uncore_pmu_register(&type->pmus[i]);
  1005. if (ret)
  1006. return ret;
  1007. }
  1008. return 0;
  1009. }
  1010. static int __init uncore_msr_pmus_register(void)
  1011. {
  1012. struct intel_uncore_type **types = uncore_msr_uncores;
  1013. int ret;
  1014. for (; *types; types++) {
  1015. ret = type_pmu_register(*types);
  1016. if (ret)
  1017. return ret;
  1018. }
  1019. return 0;
  1020. }
  1021. static int __init uncore_cpu_init(void)
  1022. {
  1023. int ret;
  1024. ret = uncore_types_init(uncore_msr_uncores, true);
  1025. if (ret)
  1026. goto err;
  1027. ret = uncore_msr_pmus_register();
  1028. if (ret)
  1029. goto err;
  1030. return 0;
  1031. err:
  1032. uncore_types_exit(uncore_msr_uncores);
  1033. uncore_msr_uncores = empty_uncore;
  1034. return ret;
  1035. }
  1036. #define X86_UNCORE_MODEL_MATCH(model, init) \
  1037. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&init }
  1038. struct intel_uncore_init_fun {
  1039. void (*cpu_init)(void);
  1040. int (*pci_init)(void);
  1041. };
  1042. static const struct intel_uncore_init_fun nhm_uncore_init __initconst = {
  1043. .cpu_init = nhm_uncore_cpu_init,
  1044. };
  1045. static const struct intel_uncore_init_fun snb_uncore_init __initconst = {
  1046. .cpu_init = snb_uncore_cpu_init,
  1047. .pci_init = snb_uncore_pci_init,
  1048. };
  1049. static const struct intel_uncore_init_fun ivb_uncore_init __initconst = {
  1050. .cpu_init = snb_uncore_cpu_init,
  1051. .pci_init = ivb_uncore_pci_init,
  1052. };
  1053. static const struct intel_uncore_init_fun hsw_uncore_init __initconst = {
  1054. .cpu_init = snb_uncore_cpu_init,
  1055. .pci_init = hsw_uncore_pci_init,
  1056. };
  1057. static const struct intel_uncore_init_fun bdw_uncore_init __initconst = {
  1058. .cpu_init = snb_uncore_cpu_init,
  1059. .pci_init = bdw_uncore_pci_init,
  1060. };
  1061. static const struct intel_uncore_init_fun snbep_uncore_init __initconst = {
  1062. .cpu_init = snbep_uncore_cpu_init,
  1063. .pci_init = snbep_uncore_pci_init,
  1064. };
  1065. static const struct intel_uncore_init_fun nhmex_uncore_init __initconst = {
  1066. .cpu_init = nhmex_uncore_cpu_init,
  1067. };
  1068. static const struct intel_uncore_init_fun ivbep_uncore_init __initconst = {
  1069. .cpu_init = ivbep_uncore_cpu_init,
  1070. .pci_init = ivbep_uncore_pci_init,
  1071. };
  1072. static const struct intel_uncore_init_fun hswep_uncore_init __initconst = {
  1073. .cpu_init = hswep_uncore_cpu_init,
  1074. .pci_init = hswep_uncore_pci_init,
  1075. };
  1076. static const struct intel_uncore_init_fun bdx_uncore_init __initconst = {
  1077. .cpu_init = bdx_uncore_cpu_init,
  1078. .pci_init = bdx_uncore_pci_init,
  1079. };
  1080. static const struct intel_uncore_init_fun knl_uncore_init __initconst = {
  1081. .cpu_init = knl_uncore_cpu_init,
  1082. .pci_init = knl_uncore_pci_init,
  1083. };
  1084. static const struct intel_uncore_init_fun skl_uncore_init __initconst = {
  1085. .cpu_init = skl_uncore_cpu_init,
  1086. .pci_init = skl_uncore_pci_init,
  1087. };
  1088. static const struct intel_uncore_init_fun skx_uncore_init __initconst = {
  1089. .cpu_init = skx_uncore_cpu_init,
  1090. .pci_init = skx_uncore_pci_init,
  1091. };
  1092. static const struct x86_cpu_id intel_uncore_match[] __initconst = {
  1093. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_NEHALEM_EP, nhm_uncore_init),
  1094. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_NEHALEM, nhm_uncore_init),
  1095. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_WESTMERE, nhm_uncore_init),
  1096. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_WESTMERE_EP, nhm_uncore_init),
  1097. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_SANDYBRIDGE, snb_uncore_init),
  1098. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_IVYBRIDGE, ivb_uncore_init),
  1099. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_HASWELL_CORE, hsw_uncore_init),
  1100. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_HASWELL_ULT, hsw_uncore_init),
  1101. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_HASWELL_GT3E, hsw_uncore_init),
  1102. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_BROADWELL_CORE, bdw_uncore_init),
  1103. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_BROADWELL_GT3E, bdw_uncore_init),
  1104. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_SANDYBRIDGE_X, snbep_uncore_init),
  1105. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_NEHALEM_EX, nhmex_uncore_init),
  1106. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_WESTMERE_EX, nhmex_uncore_init),
  1107. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_IVYBRIDGE_X, ivbep_uncore_init),
  1108. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_HASWELL_X, hswep_uncore_init),
  1109. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_BROADWELL_X, bdx_uncore_init),
  1110. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_BROADWELL_XEON_D, bdx_uncore_init),
  1111. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_XEON_PHI_KNL, knl_uncore_init),
  1112. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_XEON_PHI_KNM, knl_uncore_init),
  1113. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_SKYLAKE_DESKTOP,skl_uncore_init),
  1114. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_SKYLAKE_MOBILE, skl_uncore_init),
  1115. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_SKYLAKE_X, skx_uncore_init),
  1116. {},
  1117. };
  1118. MODULE_DEVICE_TABLE(x86cpu, intel_uncore_match);
  1119. static int __init intel_uncore_init(void)
  1120. {
  1121. const struct x86_cpu_id *id;
  1122. struct intel_uncore_init_fun *uncore_init;
  1123. int pret = 0, cret = 0, ret;
  1124. id = x86_match_cpu(intel_uncore_match);
  1125. if (!id)
  1126. return -ENODEV;
  1127. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  1128. return -ENODEV;
  1129. max_packages = topology_max_packages();
  1130. uncore_init = (struct intel_uncore_init_fun *)id->driver_data;
  1131. if (uncore_init->pci_init) {
  1132. pret = uncore_init->pci_init();
  1133. if (!pret)
  1134. pret = uncore_pci_init();
  1135. }
  1136. if (uncore_init->cpu_init) {
  1137. uncore_init->cpu_init();
  1138. cret = uncore_cpu_init();
  1139. }
  1140. if (cret && pret)
  1141. return -ENODEV;
  1142. /*
  1143. * Install callbacks. Core will call them for each online cpu.
  1144. *
  1145. * The first online cpu of each package allocates and takes
  1146. * the refcounts for all other online cpus in that package.
  1147. * If msrs are not enabled no allocation is required and
  1148. * uncore_cpu_prepare() is not called for each online cpu.
  1149. */
  1150. if (!cret) {
  1151. ret = cpuhp_setup_state(CPUHP_PERF_X86_UNCORE_PREP,
  1152. "perf/x86/intel/uncore:prepare",
  1153. uncore_cpu_prepare, NULL);
  1154. if (ret)
  1155. goto err;
  1156. } else {
  1157. cpuhp_setup_state_nocalls(CPUHP_PERF_X86_UNCORE_PREP,
  1158. "perf/x86/intel/uncore:prepare",
  1159. uncore_cpu_prepare, NULL);
  1160. }
  1161. first_init = 1;
  1162. cpuhp_setup_state(CPUHP_AP_PERF_X86_UNCORE_STARTING,
  1163. "perf/x86/uncore:starting",
  1164. uncore_cpu_starting, uncore_cpu_dying);
  1165. first_init = 0;
  1166. cpuhp_setup_state(CPUHP_AP_PERF_X86_UNCORE_ONLINE,
  1167. "perf/x86/uncore:online",
  1168. uncore_event_cpu_online, uncore_event_cpu_offline);
  1169. return 0;
  1170. err:
  1171. /* Undo box->init_box() */
  1172. on_each_cpu_mask(&uncore_cpu_mask, uncore_exit_boxes, NULL, 1);
  1173. uncore_types_exit(uncore_msr_uncores);
  1174. uncore_pci_exit();
  1175. return ret;
  1176. }
  1177. module_init(intel_uncore_init);
  1178. static void __exit intel_uncore_exit(void)
  1179. {
  1180. cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_UNCORE_ONLINE);
  1181. cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_UNCORE_STARTING);
  1182. cpuhp_remove_state_nocalls(CPUHP_PERF_X86_UNCORE_PREP);
  1183. uncore_types_exit(uncore_msr_uncores);
  1184. uncore_pci_exit();
  1185. }
  1186. module_exit(intel_uncore_exit);