cppc_acpi.c 40 KB

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