cppc_acpi.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. /*
  2. * CPPC (Collaborative Processor Performance Control) methods used by CPUfreq drivers.
  3. *
  4. * (C) Copyright 2014, 2015 Linaro Ltd.
  5. * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; version 2
  10. * of the License.
  11. *
  12. * CPPC describes a few methods for controlling CPU performance using
  13. * information from a per CPU table called CPC. This table is described in
  14. * the ACPI v5.0+ specification. The table consists of a list of
  15. * registers which may be memory mapped or hardware registers and also may
  16. * include some static integer values.
  17. *
  18. * CPU performance is on an abstract continuous scale as against a discretized
  19. * P-state scale which is tied to CPU frequency only. In brief, the basic
  20. * operation involves:
  21. *
  22. * - OS makes a CPU performance request. (Can provide min and max bounds)
  23. *
  24. * - Platform (such as BMC) is free to optimize request within requested bounds
  25. * depending on power/thermal budgets etc.
  26. *
  27. * - Platform conveys its decision back to OS
  28. *
  29. * The communication between OS and platform occurs through another medium
  30. * called (PCC) Platform Communication Channel. This is a generic mailbox like
  31. * mechanism which includes doorbell semantics to indicate register updates.
  32. * See drivers/mailbox/pcc.c for details on PCC.
  33. *
  34. * Finer details about the PCC and CPPC spec are available in the ACPI v5.1 and
  35. * above specifications.
  36. */
  37. #define pr_fmt(fmt) "ACPI CPPC: " fmt
  38. #include <linux/cpufreq.h>
  39. #include <linux/delay.h>
  40. #include <linux/ktime.h>
  41. #include <linux/rwsem.h>
  42. #include <linux/wait.h>
  43. #include <acpi/cppc_acpi.h>
  44. struct cppc_pcc_data {
  45. struct mbox_chan *pcc_channel;
  46. void __iomem *pcc_comm_addr;
  47. bool pcc_channel_acquired;
  48. ktime_t deadline;
  49. unsigned int pcc_mpar, pcc_mrtt, pcc_nominal;
  50. bool pending_pcc_write_cmd; /* Any pending/batched PCC write cmds? */
  51. bool platform_owns_pcc; /* Ownership of PCC subspace */
  52. unsigned int pcc_write_cnt; /* Running count of PCC write commands */
  53. /*
  54. * Lock to provide controlled access to the PCC channel.
  55. *
  56. * For performance critical usecases(currently cppc_set_perf)
  57. * We need to take read_lock and check if channel belongs to OSPM
  58. * before reading or writing to PCC subspace
  59. * We need to take write_lock before transferring the channel
  60. * ownership to the platform via a Doorbell
  61. * This allows us to batch a number of CPPC requests if they happen
  62. * to originate in about the same time
  63. *
  64. * For non-performance critical usecases(init)
  65. * Take write_lock for all purposes which gives exclusive access
  66. */
  67. struct rw_semaphore pcc_lock;
  68. /* Wait queue for CPUs whose requests were batched */
  69. wait_queue_head_t pcc_write_wait_q;
  70. ktime_t last_cmd_cmpl_time;
  71. ktime_t last_mpar_reset;
  72. int mpar_count;
  73. int refcount;
  74. };
  75. /* Array to represent the PCC channel per subspace id */
  76. static struct cppc_pcc_data *pcc_data[MAX_PCC_SUBSPACES];
  77. /* The cpu_pcc_subspace_idx containsper CPU subspace id */
  78. static DEFINE_PER_CPU(int, cpu_pcc_subspace_idx);
  79. /*
  80. * The cpc_desc structure contains the ACPI register details
  81. * as described in the per CPU _CPC tables. The details
  82. * include the type of register (e.g. PCC, System IO, FFH etc.)
  83. * and destination addresses which lets us READ/WRITE CPU performance
  84. * information using the appropriate I/O methods.
  85. */
  86. static DEFINE_PER_CPU(struct cpc_desc *, cpc_desc_ptr);
  87. /* pcc mapped address + header size + offset within PCC subspace */
  88. #define GET_PCC_VADDR(offs, pcc_ss_id) (pcc_data[pcc_ss_id]->pcc_comm_addr + \
  89. 0x8 + (offs))
  90. /* Check if a CPC register is in PCC */
  91. #define CPC_IN_PCC(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
  92. (cpc)->cpc_entry.reg.space_id == \
  93. ACPI_ADR_SPACE_PLATFORM_COMM)
  94. /* Evalutes to True if reg is a NULL register descriptor */
  95. #define IS_NULL_REG(reg) ((reg)->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY && \
  96. (reg)->address == 0 && \
  97. (reg)->bit_width == 0 && \
  98. (reg)->bit_offset == 0 && \
  99. (reg)->access_width == 0)
  100. /* Evalutes to True if an optional cpc field is supported */
  101. #define CPC_SUPPORTED(cpc) ((cpc)->type == ACPI_TYPE_INTEGER ? \
  102. !!(cpc)->cpc_entry.int_value : \
  103. !IS_NULL_REG(&(cpc)->cpc_entry.reg))
  104. /*
  105. * Arbitrary Retries in case the remote processor is slow to respond
  106. * to PCC commands. Keeping it high enough to cover emulators where
  107. * the processors run painfully slow.
  108. */
  109. #define NUM_RETRIES 500ULL
  110. struct cppc_attr {
  111. struct attribute attr;
  112. ssize_t (*show)(struct kobject *kobj,
  113. struct attribute *attr, char *buf);
  114. ssize_t (*store)(struct kobject *kobj,
  115. struct attribute *attr, const char *c, ssize_t count);
  116. };
  117. #define define_one_cppc_ro(_name) \
  118. static struct cppc_attr _name = \
  119. __ATTR(_name, 0444, show_##_name, NULL)
  120. #define to_cpc_desc(a) container_of(a, struct cpc_desc, kobj)
  121. #define show_cppc_data(access_fn, struct_name, member_name) \
  122. static ssize_t show_##member_name(struct kobject *kobj, \
  123. struct attribute *attr, char *buf) \
  124. { \
  125. struct cpc_desc *cpc_ptr = to_cpc_desc(kobj); \
  126. struct struct_name st_name = {0}; \
  127. int ret; \
  128. \
  129. ret = access_fn(cpc_ptr->cpu_id, &st_name); \
  130. if (ret) \
  131. return ret; \
  132. \
  133. return scnprintf(buf, PAGE_SIZE, "%llu\n", \
  134. (u64)st_name.member_name); \
  135. } \
  136. define_one_cppc_ro(member_name)
  137. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, highest_perf);
  138. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_perf);
  139. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_perf);
  140. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_nonlinear_perf);
  141. show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, reference_perf);
  142. show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, wraparound_time);
  143. static ssize_t show_feedback_ctrs(struct kobject *kobj,
  144. struct attribute *attr, char *buf)
  145. {
  146. struct cpc_desc *cpc_ptr = to_cpc_desc(kobj);
  147. struct cppc_perf_fb_ctrs fb_ctrs = {0};
  148. int ret;
  149. ret = cppc_get_perf_ctrs(cpc_ptr->cpu_id, &fb_ctrs);
  150. if (ret)
  151. return ret;
  152. return scnprintf(buf, PAGE_SIZE, "ref:%llu del:%llu\n",
  153. fb_ctrs.reference, fb_ctrs.delivered);
  154. }
  155. define_one_cppc_ro(feedback_ctrs);
  156. static struct attribute *cppc_attrs[] = {
  157. &feedback_ctrs.attr,
  158. &reference_perf.attr,
  159. &wraparound_time.attr,
  160. &highest_perf.attr,
  161. &lowest_perf.attr,
  162. &lowest_nonlinear_perf.attr,
  163. &nominal_perf.attr,
  164. NULL
  165. };
  166. static struct kobj_type cppc_ktype = {
  167. .sysfs_ops = &kobj_sysfs_ops,
  168. .default_attrs = cppc_attrs,
  169. };
  170. static int check_pcc_chan(int pcc_ss_id, bool chk_err_bit)
  171. {
  172. int ret = -EIO, status = 0;
  173. struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id];
  174. struct acpi_pcct_shared_memory __iomem *generic_comm_base =
  175. pcc_ss_data->pcc_comm_addr;
  176. ktime_t next_deadline = ktime_add(ktime_get(),
  177. pcc_ss_data->deadline);
  178. if (!pcc_ss_data->platform_owns_pcc)
  179. return 0;
  180. /* Retry in case the remote processor was too slow to catch up. */
  181. while (!ktime_after(ktime_get(), next_deadline)) {
  182. /*
  183. * Per spec, prior to boot the PCC space wil be initialized by
  184. * platform and should have set the command completion bit when
  185. * PCC can be used by OSPM
  186. */
  187. status = readw_relaxed(&generic_comm_base->status);
  188. if (status & PCC_CMD_COMPLETE_MASK) {
  189. ret = 0;
  190. if (chk_err_bit && (status & PCC_ERROR_MASK))
  191. ret = -EIO;
  192. break;
  193. }
  194. /*
  195. * Reducing the bus traffic in case this loop takes longer than
  196. * a few retries.
  197. */
  198. udelay(3);
  199. }
  200. if (likely(!ret))
  201. pcc_ss_data->platform_owns_pcc = false;
  202. else
  203. pr_err("PCC check channel failed for ss: %d. Status=%x\n",
  204. pcc_ss_id, status);
  205. return ret;
  206. }
  207. /*
  208. * This function transfers the ownership of the PCC to the platform
  209. * So it must be called while holding write_lock(pcc_lock)
  210. */
  211. static int send_pcc_cmd(int pcc_ss_id, u16 cmd)
  212. {
  213. int ret = -EIO, i;
  214. struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id];
  215. struct acpi_pcct_shared_memory *generic_comm_base =
  216. (struct acpi_pcct_shared_memory *)pcc_ss_data->pcc_comm_addr;
  217. unsigned int time_delta;
  218. /*
  219. * For CMD_WRITE we know for a fact the caller should have checked
  220. * the channel before writing to PCC space
  221. */
  222. if (cmd == CMD_READ) {
  223. /*
  224. * If there are pending cpc_writes, then we stole the channel
  225. * before write completion, so first send a WRITE command to
  226. * platform
  227. */
  228. if (pcc_ss_data->pending_pcc_write_cmd)
  229. send_pcc_cmd(pcc_ss_id, CMD_WRITE);
  230. ret = check_pcc_chan(pcc_ss_id, false);
  231. if (ret)
  232. goto end;
  233. } else /* CMD_WRITE */
  234. pcc_ss_data->pending_pcc_write_cmd = FALSE;
  235. /*
  236. * Handle the Minimum Request Turnaround Time(MRTT)
  237. * "The minimum amount of time that OSPM must wait after the completion
  238. * of a command before issuing the next command, in microseconds"
  239. */
  240. if (pcc_ss_data->pcc_mrtt) {
  241. time_delta = ktime_us_delta(ktime_get(),
  242. pcc_ss_data->last_cmd_cmpl_time);
  243. if (pcc_ss_data->pcc_mrtt > time_delta)
  244. udelay(pcc_ss_data->pcc_mrtt - time_delta);
  245. }
  246. /*
  247. * Handle the non-zero Maximum Periodic Access Rate(MPAR)
  248. * "The maximum number of periodic requests that the subspace channel can
  249. * support, reported in commands per minute. 0 indicates no limitation."
  250. *
  251. * This parameter should be ideally zero or large enough so that it can
  252. * handle maximum number of requests that all the cores in the system can
  253. * collectively generate. If it is not, we will follow the spec and just
  254. * not send the request to the platform after hitting the MPAR limit in
  255. * any 60s window
  256. */
  257. if (pcc_ss_data->pcc_mpar) {
  258. if (pcc_ss_data->mpar_count == 0) {
  259. time_delta = ktime_ms_delta(ktime_get(),
  260. pcc_ss_data->last_mpar_reset);
  261. if ((time_delta < 60 * MSEC_PER_SEC) && pcc_ss_data->last_mpar_reset) {
  262. pr_debug("PCC cmd for subspace %d not sent due to MPAR limit",
  263. pcc_ss_id);
  264. ret = -EIO;
  265. goto end;
  266. }
  267. pcc_ss_data->last_mpar_reset = ktime_get();
  268. pcc_ss_data->mpar_count = pcc_ss_data->pcc_mpar;
  269. }
  270. pcc_ss_data->mpar_count--;
  271. }
  272. /* Write to the shared comm region. */
  273. writew_relaxed(cmd, &generic_comm_base->command);
  274. /* Flip CMD COMPLETE bit */
  275. writew_relaxed(0, &generic_comm_base->status);
  276. pcc_ss_data->platform_owns_pcc = true;
  277. /* Ring doorbell */
  278. ret = mbox_send_message(pcc_ss_data->pcc_channel, &cmd);
  279. if (ret < 0) {
  280. pr_err("Err sending PCC mbox message. ss: %d cmd:%d, ret:%d\n",
  281. pcc_ss_id, cmd, ret);
  282. goto end;
  283. }
  284. /* wait for completion and check for PCC errro bit */
  285. ret = check_pcc_chan(pcc_ss_id, true);
  286. if (pcc_ss_data->pcc_mrtt)
  287. pcc_ss_data->last_cmd_cmpl_time = ktime_get();
  288. if (pcc_ss_data->pcc_channel->mbox->txdone_irq)
  289. mbox_chan_txdone(pcc_ss_data->pcc_channel, ret);
  290. else
  291. mbox_client_txdone(pcc_ss_data->pcc_channel, ret);
  292. end:
  293. if (cmd == CMD_WRITE) {
  294. if (unlikely(ret)) {
  295. for_each_possible_cpu(i) {
  296. struct cpc_desc *desc = per_cpu(cpc_desc_ptr, i);
  297. if (!desc)
  298. continue;
  299. if (desc->write_cmd_id == pcc_ss_data->pcc_write_cnt)
  300. desc->write_cmd_status = ret;
  301. }
  302. }
  303. pcc_ss_data->pcc_write_cnt++;
  304. wake_up_all(&pcc_ss_data->pcc_write_wait_q);
  305. }
  306. return ret;
  307. }
  308. static void cppc_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
  309. {
  310. if (ret < 0)
  311. pr_debug("TX did not complete: CMD sent:%x, ret:%d\n",
  312. *(u16 *)msg, ret);
  313. else
  314. pr_debug("TX completed. CMD sent:%x, ret:%d\n",
  315. *(u16 *)msg, ret);
  316. }
  317. struct mbox_client cppc_mbox_cl = {
  318. .tx_done = cppc_chan_tx_done,
  319. .knows_txdone = true,
  320. };
  321. static int acpi_get_psd(struct cpc_desc *cpc_ptr, acpi_handle handle)
  322. {
  323. int result = -EFAULT;
  324. acpi_status status = AE_OK;
  325. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  326. struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
  327. struct acpi_buffer state = {0, NULL};
  328. union acpi_object *psd = NULL;
  329. struct acpi_psd_package *pdomain;
  330. status = acpi_evaluate_object_typed(handle, "_PSD", NULL, &buffer,
  331. ACPI_TYPE_PACKAGE);
  332. if (ACPI_FAILURE(status))
  333. return -ENODEV;
  334. psd = buffer.pointer;
  335. if (!psd || psd->package.count != 1) {
  336. pr_debug("Invalid _PSD data\n");
  337. goto end;
  338. }
  339. pdomain = &(cpc_ptr->domain_info);
  340. state.length = sizeof(struct acpi_psd_package);
  341. state.pointer = pdomain;
  342. status = acpi_extract_package(&(psd->package.elements[0]),
  343. &format, &state);
  344. if (ACPI_FAILURE(status)) {
  345. pr_debug("Invalid _PSD data for CPU:%d\n", cpc_ptr->cpu_id);
  346. goto end;
  347. }
  348. if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
  349. pr_debug("Unknown _PSD:num_entries for CPU:%d\n", cpc_ptr->cpu_id);
  350. goto end;
  351. }
  352. if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
  353. pr_debug("Unknown _PSD:revision for CPU: %d\n", cpc_ptr->cpu_id);
  354. goto end;
  355. }
  356. if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
  357. pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
  358. pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
  359. pr_debug("Invalid _PSD:coord_type for CPU:%d\n", cpc_ptr->cpu_id);
  360. goto end;
  361. }
  362. result = 0;
  363. end:
  364. kfree(buffer.pointer);
  365. return result;
  366. }
  367. /**
  368. * acpi_get_psd_map - Map the CPUs in a common freq domain.
  369. * @all_cpu_data: Ptrs to CPU specific CPPC data including PSD info.
  370. *
  371. * Return: 0 for success or negative value for err.
  372. */
  373. int acpi_get_psd_map(struct cppc_cpudata **all_cpu_data)
  374. {
  375. int count_target;
  376. int retval = 0;
  377. unsigned int i, j;
  378. cpumask_var_t covered_cpus;
  379. struct cppc_cpudata *pr, *match_pr;
  380. struct acpi_psd_package *pdomain;
  381. struct acpi_psd_package *match_pdomain;
  382. struct cpc_desc *cpc_ptr, *match_cpc_ptr;
  383. if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
  384. return -ENOMEM;
  385. /*
  386. * Now that we have _PSD data from all CPUs, lets setup P-state
  387. * domain info.
  388. */
  389. for_each_possible_cpu(i) {
  390. pr = all_cpu_data[i];
  391. if (!pr)
  392. continue;
  393. if (cpumask_test_cpu(i, covered_cpus))
  394. continue;
  395. cpc_ptr = per_cpu(cpc_desc_ptr, i);
  396. if (!cpc_ptr) {
  397. retval = -EFAULT;
  398. goto err_ret;
  399. }
  400. pdomain = &(cpc_ptr->domain_info);
  401. cpumask_set_cpu(i, pr->shared_cpu_map);
  402. cpumask_set_cpu(i, covered_cpus);
  403. if (pdomain->num_processors <= 1)
  404. continue;
  405. /* Validate the Domain info */
  406. count_target = pdomain->num_processors;
  407. if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
  408. pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  409. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
  410. pr->shared_type = CPUFREQ_SHARED_TYPE_HW;
  411. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
  412. pr->shared_type = CPUFREQ_SHARED_TYPE_ANY;
  413. for_each_possible_cpu(j) {
  414. if (i == j)
  415. continue;
  416. match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
  417. if (!match_cpc_ptr) {
  418. retval = -EFAULT;
  419. goto err_ret;
  420. }
  421. match_pdomain = &(match_cpc_ptr->domain_info);
  422. if (match_pdomain->domain != pdomain->domain)
  423. continue;
  424. /* Here i and j are in the same domain */
  425. if (match_pdomain->num_processors != count_target) {
  426. retval = -EFAULT;
  427. goto err_ret;
  428. }
  429. if (pdomain->coord_type != match_pdomain->coord_type) {
  430. retval = -EFAULT;
  431. goto err_ret;
  432. }
  433. cpumask_set_cpu(j, covered_cpus);
  434. cpumask_set_cpu(j, pr->shared_cpu_map);
  435. }
  436. for_each_possible_cpu(j) {
  437. if (i == j)
  438. continue;
  439. match_pr = all_cpu_data[j];
  440. if (!match_pr)
  441. continue;
  442. match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
  443. if (!match_cpc_ptr) {
  444. retval = -EFAULT;
  445. goto err_ret;
  446. }
  447. match_pdomain = &(match_cpc_ptr->domain_info);
  448. if (match_pdomain->domain != pdomain->domain)
  449. continue;
  450. match_pr->shared_type = pr->shared_type;
  451. cpumask_copy(match_pr->shared_cpu_map,
  452. pr->shared_cpu_map);
  453. }
  454. }
  455. err_ret:
  456. for_each_possible_cpu(i) {
  457. pr = all_cpu_data[i];
  458. if (!pr)
  459. continue;
  460. /* Assume no coordination on any error parsing domain info */
  461. if (retval) {
  462. cpumask_clear(pr->shared_cpu_map);
  463. cpumask_set_cpu(i, pr->shared_cpu_map);
  464. pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  465. }
  466. }
  467. free_cpumask_var(covered_cpus);
  468. return retval;
  469. }
  470. EXPORT_SYMBOL_GPL(acpi_get_psd_map);
  471. static int register_pcc_channel(int pcc_ss_idx)
  472. {
  473. struct acpi_pcct_hw_reduced *cppc_ss;
  474. u64 usecs_lat;
  475. if (pcc_ss_idx >= 0) {
  476. pcc_data[pcc_ss_idx]->pcc_channel =
  477. pcc_mbox_request_channel(&cppc_mbox_cl, pcc_ss_idx);
  478. if (IS_ERR(pcc_data[pcc_ss_idx]->pcc_channel)) {
  479. pr_err("Failed to find PCC channel for subspace %d\n",
  480. pcc_ss_idx);
  481. return -ENODEV;
  482. }
  483. /*
  484. * The PCC mailbox controller driver should
  485. * have parsed the PCCT (global table of all
  486. * PCC channels) and stored pointers to the
  487. * subspace communication region in con_priv.
  488. */
  489. cppc_ss = (pcc_data[pcc_ss_idx]->pcc_channel)->con_priv;
  490. if (!cppc_ss) {
  491. pr_err("No PCC subspace found for %d CPPC\n",
  492. pcc_ss_idx);
  493. return -ENODEV;
  494. }
  495. /*
  496. * cppc_ss->latency is just a Nominal value. In reality
  497. * the remote processor could be much slower to reply.
  498. * So add an arbitrary amount of wait on top of Nominal.
  499. */
  500. usecs_lat = NUM_RETRIES * cppc_ss->latency;
  501. pcc_data[pcc_ss_idx]->deadline = ns_to_ktime(usecs_lat * NSEC_PER_USEC);
  502. pcc_data[pcc_ss_idx]->pcc_mrtt = cppc_ss->min_turnaround_time;
  503. pcc_data[pcc_ss_idx]->pcc_mpar = cppc_ss->max_access_rate;
  504. pcc_data[pcc_ss_idx]->pcc_nominal = cppc_ss->latency;
  505. pcc_data[pcc_ss_idx]->pcc_comm_addr =
  506. acpi_os_ioremap(cppc_ss->base_address, cppc_ss->length);
  507. if (!pcc_data[pcc_ss_idx]->pcc_comm_addr) {
  508. pr_err("Failed to ioremap PCC comm region mem for %d\n",
  509. pcc_ss_idx);
  510. return -ENOMEM;
  511. }
  512. /* Set flag so that we dont come here for each CPU. */
  513. pcc_data[pcc_ss_idx]->pcc_channel_acquired = true;
  514. }
  515. return 0;
  516. }
  517. /**
  518. * cpc_ffh_supported() - check if FFH reading supported
  519. *
  520. * Check if the architecture has support for functional fixed hardware
  521. * read/write capability.
  522. *
  523. * Return: true for supported, false for not supported
  524. */
  525. bool __weak cpc_ffh_supported(void)
  526. {
  527. return false;
  528. }
  529. /**
  530. * pcc_data_alloc() - Allocate the pcc_data memory for pcc subspace
  531. *
  532. * Check and allocate the cppc_pcc_data memory.
  533. * In some processor configurations it is possible that same subspace
  534. * is shared between multiple CPU's. This is seen especially in CPU's
  535. * with hardware multi-threading support.
  536. *
  537. * Return: 0 for success, errno for failure
  538. */
  539. int pcc_data_alloc(int pcc_ss_id)
  540. {
  541. if (pcc_ss_id < 0 || pcc_ss_id >= MAX_PCC_SUBSPACES)
  542. return -EINVAL;
  543. if (pcc_data[pcc_ss_id]) {
  544. pcc_data[pcc_ss_id]->refcount++;
  545. } else {
  546. pcc_data[pcc_ss_id] = kzalloc(sizeof(struct cppc_pcc_data),
  547. GFP_KERNEL);
  548. if (!pcc_data[pcc_ss_id])
  549. return -ENOMEM;
  550. pcc_data[pcc_ss_id]->refcount++;
  551. }
  552. return 0;
  553. }
  554. /*
  555. * An example CPC table looks like the following.
  556. *
  557. * Name(_CPC, Package()
  558. * {
  559. * 17,
  560. * NumEntries
  561. * 1,
  562. * // Revision
  563. * ResourceTemplate(){Register(PCC, 32, 0, 0x120, 2)},
  564. * // Highest Performance
  565. * ResourceTemplate(){Register(PCC, 32, 0, 0x124, 2)},
  566. * // Nominal Performance
  567. * ResourceTemplate(){Register(PCC, 32, 0, 0x128, 2)},
  568. * // Lowest Nonlinear Performance
  569. * ResourceTemplate(){Register(PCC, 32, 0, 0x12C, 2)},
  570. * // Lowest Performance
  571. * ResourceTemplate(){Register(PCC, 32, 0, 0x130, 2)},
  572. * // Guaranteed Performance Register
  573. * ResourceTemplate(){Register(PCC, 32, 0, 0x110, 2)},
  574. * // Desired Performance Register
  575. * ResourceTemplate(){Register(SystemMemory, 0, 0, 0, 0)},
  576. * ..
  577. * ..
  578. * ..
  579. *
  580. * }
  581. * Each Register() encodes how to access that specific register.
  582. * e.g. a sample PCC entry has the following encoding:
  583. *
  584. * Register (
  585. * PCC,
  586. * AddressSpaceKeyword
  587. * 8,
  588. * //RegisterBitWidth
  589. * 8,
  590. * //RegisterBitOffset
  591. * 0x30,
  592. * //RegisterAddress
  593. * 9
  594. * //AccessSize (subspace ID)
  595. * 0
  596. * )
  597. * }
  598. */
  599. /**
  600. * acpi_cppc_processor_probe - Search for per CPU _CPC objects.
  601. * @pr: Ptr to acpi_processor containing this CPUs logical Id.
  602. *
  603. * Return: 0 for success or negative value for err.
  604. */
  605. int acpi_cppc_processor_probe(struct acpi_processor *pr)
  606. {
  607. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  608. union acpi_object *out_obj, *cpc_obj;
  609. struct cpc_desc *cpc_ptr;
  610. struct cpc_reg *gas_t;
  611. struct device *cpu_dev;
  612. acpi_handle handle = pr->handle;
  613. unsigned int num_ent, i, cpc_rev;
  614. int pcc_subspace_id = -1;
  615. acpi_status status;
  616. int ret = -EFAULT;
  617. /* Parse the ACPI _CPC table for this cpu. */
  618. status = acpi_evaluate_object_typed(handle, "_CPC", NULL, &output,
  619. ACPI_TYPE_PACKAGE);
  620. if (ACPI_FAILURE(status)) {
  621. ret = -ENODEV;
  622. goto out_buf_free;
  623. }
  624. out_obj = (union acpi_object *) output.pointer;
  625. cpc_ptr = kzalloc(sizeof(struct cpc_desc), GFP_KERNEL);
  626. if (!cpc_ptr) {
  627. ret = -ENOMEM;
  628. goto out_buf_free;
  629. }
  630. /* First entry is NumEntries. */
  631. cpc_obj = &out_obj->package.elements[0];
  632. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  633. num_ent = cpc_obj->integer.value;
  634. } else {
  635. pr_debug("Unexpected entry type(%d) for NumEntries\n",
  636. cpc_obj->type);
  637. goto out_free;
  638. }
  639. /* Only support CPPCv2. Bail otherwise. */
  640. if (num_ent != CPPC_NUM_ENT) {
  641. pr_debug("Firmware exports %d entries. Expected: %d\n",
  642. num_ent, CPPC_NUM_ENT);
  643. goto out_free;
  644. }
  645. cpc_ptr->num_entries = num_ent;
  646. /* Second entry should be revision. */
  647. cpc_obj = &out_obj->package.elements[1];
  648. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  649. cpc_rev = cpc_obj->integer.value;
  650. } else {
  651. pr_debug("Unexpected entry type(%d) for Revision\n",
  652. cpc_obj->type);
  653. goto out_free;
  654. }
  655. if (cpc_rev != CPPC_REV) {
  656. pr_debug("Firmware exports revision:%d. Expected:%d\n",
  657. cpc_rev, CPPC_REV);
  658. goto out_free;
  659. }
  660. /* Iterate through remaining entries in _CPC */
  661. for (i = 2; i < num_ent; i++) {
  662. cpc_obj = &out_obj->package.elements[i];
  663. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  664. cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER;
  665. cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = cpc_obj->integer.value;
  666. } else if (cpc_obj->type == ACPI_TYPE_BUFFER) {
  667. gas_t = (struct cpc_reg *)
  668. cpc_obj->buffer.pointer;
  669. /*
  670. * The PCC Subspace index is encoded inside
  671. * the CPC table entries. The same PCC index
  672. * will be used for all the PCC entries,
  673. * so extract it only once.
  674. */
  675. if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  676. if (pcc_subspace_id < 0) {
  677. pcc_subspace_id = gas_t->access_width;
  678. if (pcc_data_alloc(pcc_subspace_id))
  679. goto out_free;
  680. } else if (pcc_subspace_id != gas_t->access_width) {
  681. pr_debug("Mismatched PCC ids.\n");
  682. goto out_free;
  683. }
  684. } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  685. if (gas_t->address) {
  686. void __iomem *addr;
  687. addr = ioremap(gas_t->address, gas_t->bit_width/8);
  688. if (!addr)
  689. goto out_free;
  690. cpc_ptr->cpc_regs[i-2].sys_mem_vaddr = addr;
  691. }
  692. } else {
  693. if (gas_t->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE || !cpc_ffh_supported()) {
  694. /* Support only PCC ,SYS MEM and FFH type regs */
  695. pr_debug("Unsupported register type: %d\n", gas_t->space_id);
  696. goto out_free;
  697. }
  698. }
  699. cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_BUFFER;
  700. memcpy(&cpc_ptr->cpc_regs[i-2].cpc_entry.reg, gas_t, sizeof(*gas_t));
  701. } else {
  702. pr_debug("Err in entry:%d in CPC table of CPU:%d \n", i, pr->id);
  703. goto out_free;
  704. }
  705. }
  706. per_cpu(cpu_pcc_subspace_idx, pr->id) = pcc_subspace_id;
  707. /* Store CPU Logical ID */
  708. cpc_ptr->cpu_id = pr->id;
  709. /* Parse PSD data for this CPU */
  710. ret = acpi_get_psd(cpc_ptr, handle);
  711. if (ret)
  712. goto out_free;
  713. /* Register PCC channel once for all PCC subspace id. */
  714. if (pcc_subspace_id >= 0 && !pcc_data[pcc_subspace_id]->pcc_channel_acquired) {
  715. ret = register_pcc_channel(pcc_subspace_id);
  716. if (ret)
  717. goto out_free;
  718. init_rwsem(&pcc_data[pcc_subspace_id]->pcc_lock);
  719. init_waitqueue_head(&pcc_data[pcc_subspace_id]->pcc_write_wait_q);
  720. }
  721. /* Everything looks okay */
  722. pr_debug("Parsed CPC struct for CPU: %d\n", pr->id);
  723. /* Add per logical CPU nodes for reading its feedback counters. */
  724. cpu_dev = get_cpu_device(pr->id);
  725. if (!cpu_dev) {
  726. ret = -EINVAL;
  727. goto out_free;
  728. }
  729. /* Plug PSD data into this CPUs CPC descriptor. */
  730. per_cpu(cpc_desc_ptr, pr->id) = cpc_ptr;
  731. ret = kobject_init_and_add(&cpc_ptr->kobj, &cppc_ktype, &cpu_dev->kobj,
  732. "acpi_cppc");
  733. if (ret) {
  734. per_cpu(cpc_desc_ptr, pr->id) = NULL;
  735. goto out_free;
  736. }
  737. kfree(output.pointer);
  738. return 0;
  739. out_free:
  740. /* Free all the mapped sys mem areas for this CPU */
  741. for (i = 2; i < cpc_ptr->num_entries; i++) {
  742. void __iomem *addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
  743. if (addr)
  744. iounmap(addr);
  745. }
  746. kfree(cpc_ptr);
  747. out_buf_free:
  748. kfree(output.pointer);
  749. return ret;
  750. }
  751. EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe);
  752. /**
  753. * acpi_cppc_processor_exit - Cleanup CPC structs.
  754. * @pr: Ptr to acpi_processor containing this CPUs logical Id.
  755. *
  756. * Return: Void
  757. */
  758. void acpi_cppc_processor_exit(struct acpi_processor *pr)
  759. {
  760. struct cpc_desc *cpc_ptr;
  761. unsigned int i;
  762. void __iomem *addr;
  763. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, pr->id);
  764. if (pcc_ss_id >=0 && pcc_data[pcc_ss_id]) {
  765. if (pcc_data[pcc_ss_id]->pcc_channel_acquired) {
  766. pcc_data[pcc_ss_id]->refcount--;
  767. if (!pcc_data[pcc_ss_id]->refcount) {
  768. pcc_mbox_free_channel(pcc_data[pcc_ss_id]->pcc_channel);
  769. pcc_data[pcc_ss_id]->pcc_channel_acquired = 0;
  770. kfree(pcc_data[pcc_ss_id]);
  771. }
  772. }
  773. }
  774. cpc_ptr = per_cpu(cpc_desc_ptr, pr->id);
  775. if (!cpc_ptr)
  776. return;
  777. /* Free all the mapped sys mem areas for this CPU */
  778. for (i = 2; i < cpc_ptr->num_entries; i++) {
  779. addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
  780. if (addr)
  781. iounmap(addr);
  782. }
  783. kobject_put(&cpc_ptr->kobj);
  784. kfree(cpc_ptr);
  785. }
  786. EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit);
  787. /**
  788. * cpc_read_ffh() - Read FFH register
  789. * @cpunum: cpu number to read
  790. * @reg: cppc register information
  791. * @val: place holder for return value
  792. *
  793. * Read bit_width bits from a specified address and bit_offset
  794. *
  795. * Return: 0 for success and error code
  796. */
  797. int __weak cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val)
  798. {
  799. return -ENOTSUPP;
  800. }
  801. /**
  802. * cpc_write_ffh() - Write FFH register
  803. * @cpunum: cpu number to write
  804. * @reg: cppc register information
  805. * @val: value to write
  806. *
  807. * Write value of bit_width bits to a specified address and bit_offset
  808. *
  809. * Return: 0 for success and error code
  810. */
  811. int __weak cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
  812. {
  813. return -ENOTSUPP;
  814. }
  815. /*
  816. * Since cpc_read and cpc_write are called while holding pcc_lock, it should be
  817. * as fast as possible. We have already mapped the PCC subspace during init, so
  818. * we can directly write to it.
  819. */
  820. static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val)
  821. {
  822. int ret_val = 0;
  823. void __iomem *vaddr = 0;
  824. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
  825. struct cpc_reg *reg = &reg_res->cpc_entry.reg;
  826. if (reg_res->type == ACPI_TYPE_INTEGER) {
  827. *val = reg_res->cpc_entry.int_value;
  828. return ret_val;
  829. }
  830. *val = 0;
  831. if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0)
  832. vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
  833. else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  834. vaddr = reg_res->sys_mem_vaddr;
  835. else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
  836. return cpc_read_ffh(cpu, reg, val);
  837. else
  838. return acpi_os_read_memory((acpi_physical_address)reg->address,
  839. val, reg->bit_width);
  840. switch (reg->bit_width) {
  841. case 8:
  842. *val = readb_relaxed(vaddr);
  843. break;
  844. case 16:
  845. *val = readw_relaxed(vaddr);
  846. break;
  847. case 32:
  848. *val = readl_relaxed(vaddr);
  849. break;
  850. case 64:
  851. *val = readq_relaxed(vaddr);
  852. break;
  853. default:
  854. pr_debug("Error: Cannot read %u bit width from PCC for ss: %d\n",
  855. reg->bit_width, pcc_ss_id);
  856. ret_val = -EFAULT;
  857. }
  858. return ret_val;
  859. }
  860. static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val)
  861. {
  862. int ret_val = 0;
  863. void __iomem *vaddr = 0;
  864. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
  865. struct cpc_reg *reg = &reg_res->cpc_entry.reg;
  866. if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0)
  867. vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
  868. else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  869. vaddr = reg_res->sys_mem_vaddr;
  870. else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
  871. return cpc_write_ffh(cpu, reg, val);
  872. else
  873. return acpi_os_write_memory((acpi_physical_address)reg->address,
  874. val, reg->bit_width);
  875. switch (reg->bit_width) {
  876. case 8:
  877. writeb_relaxed(val, vaddr);
  878. break;
  879. case 16:
  880. writew_relaxed(val, vaddr);
  881. break;
  882. case 32:
  883. writel_relaxed(val, vaddr);
  884. break;
  885. case 64:
  886. writeq_relaxed(val, vaddr);
  887. break;
  888. default:
  889. pr_debug("Error: Cannot write %u bit width to PCC for ss: %d\n",
  890. reg->bit_width, pcc_ss_id);
  891. ret_val = -EFAULT;
  892. break;
  893. }
  894. return ret_val;
  895. }
  896. /**
  897. * cppc_get_perf_caps - Get a CPUs performance capabilities.
  898. * @cpunum: CPU from which to get capabilities info.
  899. * @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h
  900. *
  901. * Return: 0 for success with perf_caps populated else -ERRNO.
  902. */
  903. int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps)
  904. {
  905. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
  906. struct cpc_register_resource *highest_reg, *lowest_reg,
  907. *lowest_non_linear_reg, *nominal_reg;
  908. u64 high, low, nom, min_nonlinear;
  909. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
  910. struct cppc_pcc_data *pcc_ss_data;
  911. int ret = 0, regs_in_pcc = 0;
  912. if (!cpc_desc || pcc_ss_id < 0) {
  913. pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
  914. return -ENODEV;
  915. }
  916. pcc_ss_data = pcc_data[pcc_ss_id];
  917. highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF];
  918. lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF];
  919. lowest_non_linear_reg = &cpc_desc->cpc_regs[LOW_NON_LINEAR_PERF];
  920. nominal_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
  921. /* Are any of the regs PCC ?*/
  922. if (CPC_IN_PCC(highest_reg) || CPC_IN_PCC(lowest_reg) ||
  923. CPC_IN_PCC(lowest_non_linear_reg) || CPC_IN_PCC(nominal_reg)) {
  924. regs_in_pcc = 1;
  925. down_write(&pcc_ss_data->pcc_lock);
  926. /* Ring doorbell once to update PCC subspace */
  927. if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
  928. ret = -EIO;
  929. goto out_err;
  930. }
  931. }
  932. cpc_read(cpunum, highest_reg, &high);
  933. perf_caps->highest_perf = high;
  934. cpc_read(cpunum, lowest_reg, &low);
  935. perf_caps->lowest_perf = low;
  936. cpc_read(cpunum, nominal_reg, &nom);
  937. perf_caps->nominal_perf = nom;
  938. cpc_read(cpunum, lowest_non_linear_reg, &min_nonlinear);
  939. perf_caps->lowest_nonlinear_perf = min_nonlinear;
  940. if (!high || !low || !nom || !min_nonlinear)
  941. ret = -EFAULT;
  942. out_err:
  943. if (regs_in_pcc)
  944. up_write(&pcc_ss_data->pcc_lock);
  945. return ret;
  946. }
  947. EXPORT_SYMBOL_GPL(cppc_get_perf_caps);
  948. /**
  949. * cppc_get_perf_ctrs - Read a CPUs performance feedback counters.
  950. * @cpunum: CPU from which to read counters.
  951. * @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h
  952. *
  953. * Return: 0 for success with perf_fb_ctrs populated else -ERRNO.
  954. */
  955. int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs)
  956. {
  957. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
  958. struct cpc_register_resource *delivered_reg, *reference_reg,
  959. *ref_perf_reg, *ctr_wrap_reg;
  960. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
  961. struct cppc_pcc_data *pcc_ss_data;
  962. u64 delivered, reference, ref_perf, ctr_wrap_time;
  963. int ret = 0, regs_in_pcc = 0;
  964. if (!cpc_desc || pcc_ss_id < 0) {
  965. pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
  966. return -ENODEV;
  967. }
  968. pcc_ss_data = pcc_data[pcc_ss_id];
  969. delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR];
  970. reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR];
  971. ref_perf_reg = &cpc_desc->cpc_regs[REFERENCE_PERF];
  972. ctr_wrap_reg = &cpc_desc->cpc_regs[CTR_WRAP_TIME];
  973. /*
  974. * If refernce perf register is not supported then we should
  975. * use the nominal perf value
  976. */
  977. if (!CPC_SUPPORTED(ref_perf_reg))
  978. ref_perf_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
  979. /* Are any of the regs PCC ?*/
  980. if (CPC_IN_PCC(delivered_reg) || CPC_IN_PCC(reference_reg) ||
  981. CPC_IN_PCC(ctr_wrap_reg) || CPC_IN_PCC(ref_perf_reg)) {
  982. down_write(&pcc_ss_data->pcc_lock);
  983. regs_in_pcc = 1;
  984. /* Ring doorbell once to update PCC subspace */
  985. if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
  986. ret = -EIO;
  987. goto out_err;
  988. }
  989. }
  990. cpc_read(cpunum, delivered_reg, &delivered);
  991. cpc_read(cpunum, reference_reg, &reference);
  992. cpc_read(cpunum, ref_perf_reg, &ref_perf);
  993. /*
  994. * Per spec, if ctr_wrap_time optional register is unsupported, then the
  995. * performance counters are assumed to never wrap during the lifetime of
  996. * platform
  997. */
  998. ctr_wrap_time = (u64)(~((u64)0));
  999. if (CPC_SUPPORTED(ctr_wrap_reg))
  1000. cpc_read(cpunum, ctr_wrap_reg, &ctr_wrap_time);
  1001. if (!delivered || !reference || !ref_perf) {
  1002. ret = -EFAULT;
  1003. goto out_err;
  1004. }
  1005. perf_fb_ctrs->delivered = delivered;
  1006. perf_fb_ctrs->reference = reference;
  1007. perf_fb_ctrs->reference_perf = ref_perf;
  1008. perf_fb_ctrs->wraparound_time = ctr_wrap_time;
  1009. out_err:
  1010. if (regs_in_pcc)
  1011. up_write(&pcc_ss_data->pcc_lock);
  1012. return ret;
  1013. }
  1014. EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs);
  1015. /**
  1016. * cppc_set_perf - Set a CPUs performance controls.
  1017. * @cpu: CPU for which to set performance controls.
  1018. * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h
  1019. *
  1020. * Return: 0 for success, -ERRNO otherwise.
  1021. */
  1022. int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)
  1023. {
  1024. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
  1025. struct cpc_register_resource *desired_reg;
  1026. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
  1027. struct cppc_pcc_data *pcc_ss_data;
  1028. int ret = 0;
  1029. if (!cpc_desc || pcc_ss_id < 0) {
  1030. pr_debug("No CPC descriptor for CPU:%d\n", cpu);
  1031. return -ENODEV;
  1032. }
  1033. pcc_ss_data = pcc_data[pcc_ss_id];
  1034. desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
  1035. /*
  1036. * This is Phase-I where we want to write to CPC registers
  1037. * -> We want all CPUs to be able to execute this phase in parallel
  1038. *
  1039. * Since read_lock can be acquired by multiple CPUs simultaneously we
  1040. * achieve that goal here
  1041. */
  1042. if (CPC_IN_PCC(desired_reg)) {
  1043. down_read(&pcc_ss_data->pcc_lock); /* BEGIN Phase-I */
  1044. if (pcc_ss_data->platform_owns_pcc) {
  1045. ret = check_pcc_chan(pcc_ss_id, false);
  1046. if (ret) {
  1047. up_read(&pcc_ss_data->pcc_lock);
  1048. return ret;
  1049. }
  1050. }
  1051. /*
  1052. * Update the pending_write to make sure a PCC CMD_READ will not
  1053. * arrive and steal the channel during the switch to write lock
  1054. */
  1055. pcc_ss_data->pending_pcc_write_cmd = true;
  1056. cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt;
  1057. cpc_desc->write_cmd_status = 0;
  1058. }
  1059. /*
  1060. * Skip writing MIN/MAX until Linux knows how to come up with
  1061. * useful values.
  1062. */
  1063. cpc_write(cpu, desired_reg, perf_ctrls->desired_perf);
  1064. if (CPC_IN_PCC(desired_reg))
  1065. up_read(&pcc_ss_data->pcc_lock); /* END Phase-I */
  1066. /*
  1067. * This is Phase-II where we transfer the ownership of PCC to Platform
  1068. *
  1069. * Short Summary: Basically if we think of a group of cppc_set_perf
  1070. * requests that happened in short overlapping interval. The last CPU to
  1071. * come out of Phase-I will enter Phase-II and ring the doorbell.
  1072. *
  1073. * We have the following requirements for Phase-II:
  1074. * 1. We want to execute Phase-II only when there are no CPUs
  1075. * currently executing in Phase-I
  1076. * 2. Once we start Phase-II we want to avoid all other CPUs from
  1077. * entering Phase-I.
  1078. * 3. We want only one CPU among all those who went through Phase-I
  1079. * to run phase-II
  1080. *
  1081. * If write_trylock fails to get the lock and doesn't transfer the
  1082. * PCC ownership to the platform, then one of the following will be TRUE
  1083. * 1. There is at-least one CPU in Phase-I which will later execute
  1084. * write_trylock, so the CPUs in Phase-I will be responsible for
  1085. * executing the Phase-II.
  1086. * 2. Some other CPU has beaten this CPU to successfully execute the
  1087. * write_trylock and has already acquired the write_lock. We know for a
  1088. * fact it(other CPU acquiring the write_lock) couldn't have happened
  1089. * before this CPU's Phase-I as we held the read_lock.
  1090. * 3. Some other CPU executing pcc CMD_READ has stolen the
  1091. * down_write, in which case, send_pcc_cmd will check for pending
  1092. * CMD_WRITE commands by checking the pending_pcc_write_cmd.
  1093. * So this CPU can be certain that its request will be delivered
  1094. * So in all cases, this CPU knows that its request will be delivered
  1095. * by another CPU and can return
  1096. *
  1097. * After getting the down_write we still need to check for
  1098. * pending_pcc_write_cmd to take care of the following scenario
  1099. * The thread running this code could be scheduled out between
  1100. * Phase-I and Phase-II. Before it is scheduled back on, another CPU
  1101. * could have delivered the request to Platform by triggering the
  1102. * doorbell and transferred the ownership of PCC to platform. So this
  1103. * avoids triggering an unnecessary doorbell and more importantly before
  1104. * triggering the doorbell it makes sure that the PCC channel ownership
  1105. * is still with OSPM.
  1106. * pending_pcc_write_cmd can also be cleared by a different CPU, if
  1107. * there was a pcc CMD_READ waiting on down_write and it steals the lock
  1108. * before the pcc CMD_WRITE is completed. pcc_send_cmd checks for this
  1109. * case during a CMD_READ and if there are pending writes it delivers
  1110. * the write command before servicing the read command
  1111. */
  1112. if (CPC_IN_PCC(desired_reg)) {
  1113. if (down_write_trylock(&pcc_ss_data->pcc_lock)) {/* BEGIN Phase-II */
  1114. /* Update only if there are pending write commands */
  1115. if (pcc_ss_data->pending_pcc_write_cmd)
  1116. send_pcc_cmd(pcc_ss_id, CMD_WRITE);
  1117. up_write(&pcc_ss_data->pcc_lock); /* END Phase-II */
  1118. } else
  1119. /* Wait until pcc_write_cnt is updated by send_pcc_cmd */
  1120. wait_event(pcc_ss_data->pcc_write_wait_q,
  1121. cpc_desc->write_cmd_id != pcc_ss_data->pcc_write_cnt);
  1122. /* send_pcc_cmd updates the status in case of failure */
  1123. ret = cpc_desc->write_cmd_status;
  1124. }
  1125. return ret;
  1126. }
  1127. EXPORT_SYMBOL_GPL(cppc_set_perf);
  1128. /**
  1129. * cppc_get_transition_latency - returns frequency transition latency in ns
  1130. *
  1131. * ACPI CPPC does not explicitly specifiy how a platform can specify the
  1132. * transition latency for perfromance change requests. The closest we have
  1133. * is the timing information from the PCCT tables which provides the info
  1134. * on the number and frequency of PCC commands the platform can handle.
  1135. */
  1136. unsigned int cppc_get_transition_latency(int cpu_num)
  1137. {
  1138. /*
  1139. * Expected transition latency is based on the PCCT timing values
  1140. * Below are definition from ACPI spec:
  1141. * pcc_nominal- Expected latency to process a command, in microseconds
  1142. * pcc_mpar - The maximum number of periodic requests that the subspace
  1143. * channel can support, reported in commands per minute. 0
  1144. * indicates no limitation.
  1145. * pcc_mrtt - The minimum amount of time that OSPM must wait after the
  1146. * completion of a command before issuing the next command,
  1147. * in microseconds.
  1148. */
  1149. unsigned int latency_ns = 0;
  1150. struct cpc_desc *cpc_desc;
  1151. struct cpc_register_resource *desired_reg;
  1152. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu_num);
  1153. struct cppc_pcc_data *pcc_ss_data;
  1154. cpc_desc = per_cpu(cpc_desc_ptr, cpu_num);
  1155. if (!cpc_desc)
  1156. return CPUFREQ_ETERNAL;
  1157. desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
  1158. if (!CPC_IN_PCC(desired_reg))
  1159. return CPUFREQ_ETERNAL;
  1160. if (pcc_ss_id < 0)
  1161. return CPUFREQ_ETERNAL;
  1162. pcc_ss_data = pcc_data[pcc_ss_id];
  1163. if (pcc_ss_data->pcc_mpar)
  1164. latency_ns = 60 * (1000 * 1000 * 1000 / pcc_ss_data->pcc_mpar);
  1165. latency_ns = max(latency_ns, pcc_ss_data->pcc_nominal * 1000);
  1166. latency_ns = max(latency_ns, pcc_ss_data->pcc_mrtt * 1000);
  1167. return latency_ns;
  1168. }
  1169. EXPORT_SYMBOL_GPL(cppc_get_transition_latency);