uncore.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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_free_boxes(struct intel_uncore_pmu *pmu)
  622. {
  623. int pkg;
  624. for (pkg = 0; pkg < max_packages; pkg++)
  625. kfree(pmu->boxes[pkg]);
  626. kfree(pmu->boxes);
  627. }
  628. static void uncore_type_exit(struct intel_uncore_type *type)
  629. {
  630. struct intel_uncore_pmu *pmu = type->pmus;
  631. int i;
  632. if (pmu) {
  633. for (i = 0; i < type->num_boxes; i++, pmu++) {
  634. uncore_pmu_unregister(pmu);
  635. uncore_free_boxes(pmu);
  636. }
  637. kfree(type->pmus);
  638. type->pmus = NULL;
  639. }
  640. kfree(type->events_group);
  641. type->events_group = NULL;
  642. }
  643. static void uncore_types_exit(struct intel_uncore_type **types)
  644. {
  645. for (; *types; types++)
  646. uncore_type_exit(*types);
  647. }
  648. static int __init uncore_type_init(struct intel_uncore_type *type, bool setid)
  649. {
  650. struct intel_uncore_pmu *pmus;
  651. struct attribute_group *attr_group;
  652. struct attribute **attrs;
  653. size_t size;
  654. int i, j;
  655. pmus = kzalloc(sizeof(*pmus) * type->num_boxes, GFP_KERNEL);
  656. if (!pmus)
  657. return -ENOMEM;
  658. size = max_packages * sizeof(struct intel_uncore_box *);
  659. for (i = 0; i < type->num_boxes; i++) {
  660. pmus[i].func_id = setid ? i : -1;
  661. pmus[i].pmu_idx = i;
  662. pmus[i].type = type;
  663. pmus[i].boxes = kzalloc(size, GFP_KERNEL);
  664. if (!pmus[i].boxes)
  665. return -ENOMEM;
  666. }
  667. type->pmus = pmus;
  668. type->unconstrainted = (struct event_constraint)
  669. __EVENT_CONSTRAINT(0, (1ULL << type->num_counters) - 1,
  670. 0, type->num_counters, 0, 0);
  671. if (type->event_descs) {
  672. for (i = 0; type->event_descs[i].attr.attr.name; i++);
  673. attr_group = kzalloc(sizeof(struct attribute *) * (i + 1) +
  674. sizeof(*attr_group), GFP_KERNEL);
  675. if (!attr_group)
  676. return -ENOMEM;
  677. attrs = (struct attribute **)(attr_group + 1);
  678. attr_group->name = "events";
  679. attr_group->attrs = attrs;
  680. for (j = 0; j < i; j++)
  681. attrs[j] = &type->event_descs[j].attr.attr;
  682. type->events_group = attr_group;
  683. }
  684. type->pmu_group = &uncore_pmu_attr_group;
  685. return 0;
  686. }
  687. static int __init
  688. uncore_types_init(struct intel_uncore_type **types, bool setid)
  689. {
  690. int ret;
  691. for (; *types; types++) {
  692. ret = uncore_type_init(*types, setid);
  693. if (ret)
  694. return ret;
  695. }
  696. return 0;
  697. }
  698. /*
  699. * add a pci uncore device
  700. */
  701. static int uncore_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  702. {
  703. struct intel_uncore_type *type;
  704. struct intel_uncore_pmu *pmu = NULL;
  705. struct intel_uncore_box *box;
  706. int phys_id, pkg, ret;
  707. phys_id = uncore_pcibus_to_physid(pdev->bus);
  708. if (phys_id < 0)
  709. return -ENODEV;
  710. pkg = topology_phys_to_logical_pkg(phys_id);
  711. if (pkg < 0)
  712. return -EINVAL;
  713. if (UNCORE_PCI_DEV_TYPE(id->driver_data) == UNCORE_EXTRA_PCI_DEV) {
  714. int idx = UNCORE_PCI_DEV_IDX(id->driver_data);
  715. uncore_extra_pci_dev[pkg].dev[idx] = pdev;
  716. pci_set_drvdata(pdev, NULL);
  717. return 0;
  718. }
  719. type = uncore_pci_uncores[UNCORE_PCI_DEV_TYPE(id->driver_data)];
  720. /*
  721. * Some platforms, e.g. Knights Landing, use a common PCI device ID
  722. * for multiple instances of an uncore PMU device type. We should check
  723. * PCI slot and func to indicate the uncore box.
  724. */
  725. if (id->driver_data & ~0xffff) {
  726. struct pci_driver *pci_drv = pdev->driver;
  727. const struct pci_device_id *ids = pci_drv->id_table;
  728. unsigned int devfn;
  729. while (ids && ids->vendor) {
  730. if ((ids->vendor == pdev->vendor) &&
  731. (ids->device == pdev->device)) {
  732. devfn = PCI_DEVFN(UNCORE_PCI_DEV_DEV(ids->driver_data),
  733. UNCORE_PCI_DEV_FUNC(ids->driver_data));
  734. if (devfn == pdev->devfn) {
  735. pmu = &type->pmus[UNCORE_PCI_DEV_IDX(ids->driver_data)];
  736. break;
  737. }
  738. }
  739. ids++;
  740. }
  741. if (pmu == NULL)
  742. return -ENODEV;
  743. } else {
  744. /*
  745. * for performance monitoring unit with multiple boxes,
  746. * each box has a different function id.
  747. */
  748. pmu = &type->pmus[UNCORE_PCI_DEV_IDX(id->driver_data)];
  749. }
  750. if (WARN_ON_ONCE(pmu->boxes[pkg] != NULL))
  751. return -EINVAL;
  752. box = uncore_alloc_box(type, NUMA_NO_NODE);
  753. if (!box)
  754. return -ENOMEM;
  755. if (pmu->func_id < 0)
  756. pmu->func_id = pdev->devfn;
  757. else
  758. WARN_ON_ONCE(pmu->func_id != pdev->devfn);
  759. atomic_inc(&box->refcnt);
  760. box->pci_phys_id = phys_id;
  761. box->pkgid = pkg;
  762. box->pci_dev = pdev;
  763. box->pmu = pmu;
  764. uncore_box_init(box);
  765. pci_set_drvdata(pdev, box);
  766. pmu->boxes[pkg] = box;
  767. if (atomic_inc_return(&pmu->activeboxes) > 1)
  768. return 0;
  769. /* First active box registers the pmu */
  770. ret = uncore_pmu_register(pmu);
  771. if (ret) {
  772. pci_set_drvdata(pdev, NULL);
  773. pmu->boxes[pkg] = NULL;
  774. uncore_box_exit(box);
  775. kfree(box);
  776. }
  777. return ret;
  778. }
  779. static void uncore_pci_remove(struct pci_dev *pdev)
  780. {
  781. struct intel_uncore_box *box;
  782. struct intel_uncore_pmu *pmu;
  783. int i, phys_id, pkg;
  784. phys_id = uncore_pcibus_to_physid(pdev->bus);
  785. pkg = topology_phys_to_logical_pkg(phys_id);
  786. box = pci_get_drvdata(pdev);
  787. if (!box) {
  788. for (i = 0; i < UNCORE_EXTRA_PCI_DEV_MAX; i++) {
  789. if (uncore_extra_pci_dev[pkg].dev[i] == pdev) {
  790. uncore_extra_pci_dev[pkg].dev[i] = NULL;
  791. break;
  792. }
  793. }
  794. WARN_ON_ONCE(i >= UNCORE_EXTRA_PCI_DEV_MAX);
  795. return;
  796. }
  797. pmu = box->pmu;
  798. if (WARN_ON_ONCE(phys_id != box->pci_phys_id))
  799. return;
  800. pci_set_drvdata(pdev, NULL);
  801. pmu->boxes[pkg] = NULL;
  802. if (atomic_dec_return(&pmu->activeboxes) == 0)
  803. uncore_pmu_unregister(pmu);
  804. uncore_box_exit(box);
  805. kfree(box);
  806. }
  807. static int __init uncore_pci_init(void)
  808. {
  809. size_t size;
  810. int ret;
  811. size = max_packages * sizeof(struct pci_extra_dev);
  812. uncore_extra_pci_dev = kzalloc(size, GFP_KERNEL);
  813. if (!uncore_extra_pci_dev) {
  814. ret = -ENOMEM;
  815. goto err;
  816. }
  817. ret = uncore_types_init(uncore_pci_uncores, false);
  818. if (ret)
  819. goto errtype;
  820. uncore_pci_driver->probe = uncore_pci_probe;
  821. uncore_pci_driver->remove = uncore_pci_remove;
  822. ret = pci_register_driver(uncore_pci_driver);
  823. if (ret)
  824. goto errtype;
  825. pcidrv_registered = true;
  826. return 0;
  827. errtype:
  828. uncore_types_exit(uncore_pci_uncores);
  829. kfree(uncore_extra_pci_dev);
  830. uncore_extra_pci_dev = NULL;
  831. uncore_free_pcibus_map();
  832. err:
  833. uncore_pci_uncores = empty_uncore;
  834. return ret;
  835. }
  836. static void uncore_pci_exit(void)
  837. {
  838. if (pcidrv_registered) {
  839. pcidrv_registered = false;
  840. pci_unregister_driver(uncore_pci_driver);
  841. uncore_types_exit(uncore_pci_uncores);
  842. kfree(uncore_extra_pci_dev);
  843. uncore_free_pcibus_map();
  844. }
  845. }
  846. static int uncore_cpu_dying(unsigned int cpu)
  847. {
  848. struct intel_uncore_type *type, **types = uncore_msr_uncores;
  849. struct intel_uncore_pmu *pmu;
  850. struct intel_uncore_box *box;
  851. int i, pkg;
  852. pkg = topology_logical_package_id(cpu);
  853. for (; *types; types++) {
  854. type = *types;
  855. pmu = type->pmus;
  856. for (i = 0; i < type->num_boxes; i++, pmu++) {
  857. box = pmu->boxes[pkg];
  858. if (box && atomic_dec_return(&box->refcnt) == 0)
  859. uncore_box_exit(box);
  860. }
  861. }
  862. return 0;
  863. }
  864. static int uncore_cpu_starting(unsigned int cpu)
  865. {
  866. struct intel_uncore_type *type, **types = uncore_msr_uncores;
  867. struct intel_uncore_pmu *pmu;
  868. struct intel_uncore_box *box;
  869. int i, pkg;
  870. pkg = topology_logical_package_id(cpu);
  871. for (; *types; types++) {
  872. type = *types;
  873. pmu = type->pmus;
  874. for (i = 0; i < type->num_boxes; i++, pmu++) {
  875. box = pmu->boxes[pkg];
  876. if (!box)
  877. continue;
  878. /* The first cpu on a package activates the box */
  879. if (atomic_inc_return(&box->refcnt) == 1)
  880. uncore_box_init(box);
  881. }
  882. }
  883. return 0;
  884. }
  885. static int uncore_cpu_prepare(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;
  891. pkg = topology_logical_package_id(cpu);
  892. for (; *types; types++) {
  893. type = *types;
  894. pmu = type->pmus;
  895. for (i = 0; i < type->num_boxes; i++, pmu++) {
  896. if (pmu->boxes[pkg])
  897. continue;
  898. /* First cpu of a package allocates the box */
  899. box = uncore_alloc_box(type, cpu_to_node(cpu));
  900. if (!box)
  901. return -ENOMEM;
  902. box->pmu = pmu;
  903. box->pkgid = pkg;
  904. pmu->boxes[pkg] = box;
  905. }
  906. }
  907. return 0;
  908. }
  909. static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu,
  910. int new_cpu)
  911. {
  912. struct intel_uncore_pmu *pmu = type->pmus;
  913. struct intel_uncore_box *box;
  914. int i, pkg;
  915. pkg = topology_logical_package_id(old_cpu < 0 ? new_cpu : old_cpu);
  916. for (i = 0; i < type->num_boxes; i++, pmu++) {
  917. box = pmu->boxes[pkg];
  918. if (!box)
  919. continue;
  920. if (old_cpu < 0) {
  921. WARN_ON_ONCE(box->cpu != -1);
  922. box->cpu = new_cpu;
  923. continue;
  924. }
  925. WARN_ON_ONCE(box->cpu != old_cpu);
  926. box->cpu = -1;
  927. if (new_cpu < 0)
  928. continue;
  929. uncore_pmu_cancel_hrtimer(box);
  930. perf_pmu_migrate_context(&pmu->pmu, old_cpu, new_cpu);
  931. box->cpu = new_cpu;
  932. }
  933. }
  934. static void uncore_change_context(struct intel_uncore_type **uncores,
  935. int old_cpu, int new_cpu)
  936. {
  937. for (; *uncores; uncores++)
  938. uncore_change_type_ctx(*uncores, old_cpu, new_cpu);
  939. }
  940. static int uncore_event_cpu_offline(unsigned int cpu)
  941. {
  942. int target;
  943. /* Check if exiting cpu is used for collecting uncore events */
  944. if (!cpumask_test_and_clear_cpu(cpu, &uncore_cpu_mask))
  945. return 0;
  946. /* Find a new cpu to collect uncore events */
  947. target = cpumask_any_but(topology_core_cpumask(cpu), cpu);
  948. /* Migrate uncore events to the new target */
  949. if (target < nr_cpu_ids)
  950. cpumask_set_cpu(target, &uncore_cpu_mask);
  951. else
  952. target = -1;
  953. uncore_change_context(uncore_msr_uncores, cpu, target);
  954. uncore_change_context(uncore_pci_uncores, cpu, target);
  955. return 0;
  956. }
  957. static int uncore_event_cpu_online(unsigned int cpu)
  958. {
  959. int target;
  960. /*
  961. * Check if there is an online cpu in the package
  962. * which collects uncore events already.
  963. */
  964. target = cpumask_any_and(&uncore_cpu_mask, topology_core_cpumask(cpu));
  965. if (target < nr_cpu_ids)
  966. return 0;
  967. cpumask_set_cpu(cpu, &uncore_cpu_mask);
  968. uncore_change_context(uncore_msr_uncores, -1, cpu);
  969. uncore_change_context(uncore_pci_uncores, -1, cpu);
  970. return 0;
  971. }
  972. static int __init type_pmu_register(struct intel_uncore_type *type)
  973. {
  974. int i, ret;
  975. for (i = 0; i < type->num_boxes; i++) {
  976. ret = uncore_pmu_register(&type->pmus[i]);
  977. if (ret)
  978. return ret;
  979. }
  980. return 0;
  981. }
  982. static int __init uncore_msr_pmus_register(void)
  983. {
  984. struct intel_uncore_type **types = uncore_msr_uncores;
  985. int ret;
  986. for (; *types; types++) {
  987. ret = type_pmu_register(*types);
  988. if (ret)
  989. return ret;
  990. }
  991. return 0;
  992. }
  993. static int __init uncore_cpu_init(void)
  994. {
  995. int ret;
  996. ret = uncore_types_init(uncore_msr_uncores, true);
  997. if (ret)
  998. goto err;
  999. ret = uncore_msr_pmus_register();
  1000. if (ret)
  1001. goto err;
  1002. return 0;
  1003. err:
  1004. uncore_types_exit(uncore_msr_uncores);
  1005. uncore_msr_uncores = empty_uncore;
  1006. return ret;
  1007. }
  1008. #define X86_UNCORE_MODEL_MATCH(model, init) \
  1009. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&init }
  1010. struct intel_uncore_init_fun {
  1011. void (*cpu_init)(void);
  1012. int (*pci_init)(void);
  1013. };
  1014. static const struct intel_uncore_init_fun nhm_uncore_init __initconst = {
  1015. .cpu_init = nhm_uncore_cpu_init,
  1016. };
  1017. static const struct intel_uncore_init_fun snb_uncore_init __initconst = {
  1018. .cpu_init = snb_uncore_cpu_init,
  1019. .pci_init = snb_uncore_pci_init,
  1020. };
  1021. static const struct intel_uncore_init_fun ivb_uncore_init __initconst = {
  1022. .cpu_init = snb_uncore_cpu_init,
  1023. .pci_init = ivb_uncore_pci_init,
  1024. };
  1025. static const struct intel_uncore_init_fun hsw_uncore_init __initconst = {
  1026. .cpu_init = snb_uncore_cpu_init,
  1027. .pci_init = hsw_uncore_pci_init,
  1028. };
  1029. static const struct intel_uncore_init_fun bdw_uncore_init __initconst = {
  1030. .cpu_init = snb_uncore_cpu_init,
  1031. .pci_init = bdw_uncore_pci_init,
  1032. };
  1033. static const struct intel_uncore_init_fun snbep_uncore_init __initconst = {
  1034. .cpu_init = snbep_uncore_cpu_init,
  1035. .pci_init = snbep_uncore_pci_init,
  1036. };
  1037. static const struct intel_uncore_init_fun nhmex_uncore_init __initconst = {
  1038. .cpu_init = nhmex_uncore_cpu_init,
  1039. };
  1040. static const struct intel_uncore_init_fun ivbep_uncore_init __initconst = {
  1041. .cpu_init = ivbep_uncore_cpu_init,
  1042. .pci_init = ivbep_uncore_pci_init,
  1043. };
  1044. static const struct intel_uncore_init_fun hswep_uncore_init __initconst = {
  1045. .cpu_init = hswep_uncore_cpu_init,
  1046. .pci_init = hswep_uncore_pci_init,
  1047. };
  1048. static const struct intel_uncore_init_fun bdx_uncore_init __initconst = {
  1049. .cpu_init = bdx_uncore_cpu_init,
  1050. .pci_init = bdx_uncore_pci_init,
  1051. };
  1052. static const struct intel_uncore_init_fun knl_uncore_init __initconst = {
  1053. .cpu_init = knl_uncore_cpu_init,
  1054. .pci_init = knl_uncore_pci_init,
  1055. };
  1056. static const struct intel_uncore_init_fun skl_uncore_init __initconst = {
  1057. .cpu_init = skl_uncore_cpu_init,
  1058. .pci_init = skl_uncore_pci_init,
  1059. };
  1060. static const struct intel_uncore_init_fun skx_uncore_init __initconst = {
  1061. .cpu_init = skx_uncore_cpu_init,
  1062. .pci_init = skx_uncore_pci_init,
  1063. };
  1064. static const struct x86_cpu_id intel_uncore_match[] __initconst = {
  1065. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_NEHALEM_EP, nhm_uncore_init),
  1066. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_NEHALEM, nhm_uncore_init),
  1067. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_WESTMERE, nhm_uncore_init),
  1068. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_WESTMERE_EP, nhm_uncore_init),
  1069. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_SANDYBRIDGE, snb_uncore_init),
  1070. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_IVYBRIDGE, ivb_uncore_init),
  1071. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_HASWELL_CORE, hsw_uncore_init),
  1072. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_HASWELL_ULT, hsw_uncore_init),
  1073. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_HASWELL_GT3E, hsw_uncore_init),
  1074. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_BROADWELL_CORE, bdw_uncore_init),
  1075. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_BROADWELL_GT3E, bdw_uncore_init),
  1076. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_SANDYBRIDGE_X, snbep_uncore_init),
  1077. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_NEHALEM_EX, nhmex_uncore_init),
  1078. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_WESTMERE_EX, nhmex_uncore_init),
  1079. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_IVYBRIDGE_X, ivbep_uncore_init),
  1080. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_HASWELL_X, hswep_uncore_init),
  1081. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_BROADWELL_X, bdx_uncore_init),
  1082. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_BROADWELL_XEON_D, bdx_uncore_init),
  1083. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_XEON_PHI_KNL, knl_uncore_init),
  1084. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_XEON_PHI_KNM, knl_uncore_init),
  1085. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_SKYLAKE_DESKTOP,skl_uncore_init),
  1086. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_SKYLAKE_MOBILE, skl_uncore_init),
  1087. X86_UNCORE_MODEL_MATCH(INTEL_FAM6_SKYLAKE_X, skx_uncore_init),
  1088. {},
  1089. };
  1090. MODULE_DEVICE_TABLE(x86cpu, intel_uncore_match);
  1091. static int __init intel_uncore_init(void)
  1092. {
  1093. const struct x86_cpu_id *id;
  1094. struct intel_uncore_init_fun *uncore_init;
  1095. int pret = 0, cret = 0, ret;
  1096. id = x86_match_cpu(intel_uncore_match);
  1097. if (!id)
  1098. return -ENODEV;
  1099. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  1100. return -ENODEV;
  1101. max_packages = topology_max_packages();
  1102. uncore_init = (struct intel_uncore_init_fun *)id->driver_data;
  1103. if (uncore_init->pci_init) {
  1104. pret = uncore_init->pci_init();
  1105. if (!pret)
  1106. pret = uncore_pci_init();
  1107. }
  1108. if (uncore_init->cpu_init) {
  1109. uncore_init->cpu_init();
  1110. cret = uncore_cpu_init();
  1111. }
  1112. if (cret && pret)
  1113. return -ENODEV;
  1114. /*
  1115. * Install callbacks. Core will call them for each online cpu.
  1116. *
  1117. * The first online cpu of each package allocates and takes
  1118. * the refcounts for all other online cpus in that package.
  1119. * If msrs are not enabled no allocation is required and
  1120. * uncore_cpu_prepare() is not called for each online cpu.
  1121. */
  1122. if (!cret) {
  1123. ret = cpuhp_setup_state(CPUHP_PERF_X86_UNCORE_PREP,
  1124. "PERF_X86_UNCORE_PREP",
  1125. uncore_cpu_prepare, NULL);
  1126. if (ret)
  1127. goto err;
  1128. } else {
  1129. cpuhp_setup_state_nocalls(CPUHP_PERF_X86_UNCORE_PREP,
  1130. "PERF_X86_UNCORE_PREP",
  1131. uncore_cpu_prepare, NULL);
  1132. }
  1133. cpuhp_setup_state(CPUHP_AP_PERF_X86_UNCORE_STARTING,
  1134. "AP_PERF_X86_UNCORE_STARTING",
  1135. uncore_cpu_starting, uncore_cpu_dying);
  1136. cpuhp_setup_state(CPUHP_AP_PERF_X86_UNCORE_ONLINE,
  1137. "AP_PERF_X86_UNCORE_ONLINE",
  1138. uncore_event_cpu_online, uncore_event_cpu_offline);
  1139. return 0;
  1140. err:
  1141. uncore_types_exit(uncore_msr_uncores);
  1142. uncore_pci_exit();
  1143. return ret;
  1144. }
  1145. module_init(intel_uncore_init);
  1146. static void __exit intel_uncore_exit(void)
  1147. {
  1148. cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_UNCORE_ONLINE);
  1149. cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_UNCORE_STARTING);
  1150. cpuhp_remove_state_nocalls(CPUHP_PERF_X86_UNCORE_PREP);
  1151. uncore_types_exit(uncore_msr_uncores);
  1152. uncore_pci_exit();
  1153. }
  1154. module_exit(intel_uncore_exit);