blob: dd521a181f23bdb5237607e65fabbe7df6d2988a [file] [log] [blame]
Geoff Levand00a3e2e2006-11-23 00:46:58 +01001/*
Geoff Levandca942972007-10-07 07:35:43 +10002 * PS3 flash memory os area.
Geoff Levand00a3e2e2006-11-23 00:46:58 +01003 *
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006 Sony Corp.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/kernel.h>
22#include <linux/io.h>
Geoff Levand418ef202007-10-07 07:35:45 +100023#include <linux/workqueue.h>
Geoff Levandef2ac632007-10-09 11:07:24 +100024#include <linux/fs.h>
25#include <linux/syscalls.h>
Geoff Levandec5d2df2007-12-24 04:41:01 +110026#include <linux/ctype.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080027#include <linux/lmb.h>
Michael Ellermane988a132008-02-14 11:37:50 +110028#include <linux/of.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Geoff Levand00a3e2e2006-11-23 00:46:58 +010030
David S. Millerd9b2b2a2008-02-13 16:56:49 -080031#include <asm/prom.h>
Geoff Levand00a3e2e2006-11-23 00:46:58 +010032
33#include "platform.h"
34
35enum {
36 OS_AREA_SEGMENT_SIZE = 0X200,
37};
38
Geoff Levandca942972007-10-07 07:35:43 +100039enum os_area_ldr_format {
Geoff Levand00a3e2e2006-11-23 00:46:58 +010040 HEADER_LDR_FORMAT_RAW = 0,
41 HEADER_LDR_FORMAT_GZIP = 1,
42};
43
Geoff Levandec5d2df2007-12-24 04:41:01 +110044#define OS_AREA_HEADER_MAGIC_NUM "cell_ext_os_area"
45
Geoff Levand00a3e2e2006-11-23 00:46:58 +010046/**
47 * struct os_area_header - os area header segment.
48 * @magic_num: Always 'cell_ext_os_area'.
49 * @hdr_version: Header format version number.
Geoff Levandef2ac632007-10-09 11:07:24 +100050 * @db_area_offset: Starting segment number of other os database area.
Geoff Levand00a3e2e2006-11-23 00:46:58 +010051 * @ldr_area_offset: Starting segment number of bootloader image area.
52 * @ldr_format: HEADER_LDR_FORMAT flag.
53 * @ldr_size: Size of bootloader image in bytes.
54 *
55 * Note that the docs refer to area offsets. These are offsets in units of
56 * segments from the start of the os area (top of the header). These are
57 * better thought of as segment numbers. The os area of the os area is
58 * reserved for the os image.
59 */
60
61struct os_area_header {
Geoff Levandca942972007-10-07 07:35:43 +100062 u8 magic_num[16];
Geoff Levand00a3e2e2006-11-23 00:46:58 +010063 u32 hdr_version;
Geoff Levandef2ac632007-10-09 11:07:24 +100064 u32 db_area_offset;
Geoff Levand00a3e2e2006-11-23 00:46:58 +010065 u32 ldr_area_offset;
66 u32 _reserved_1;
67 u32 ldr_format;
68 u32 ldr_size;
69 u32 _reserved_2[6];
Geoff Levanda8229a92007-01-26 19:07:56 -080070};
Geoff Levand00a3e2e2006-11-23 00:46:58 +010071
Geoff Levandca942972007-10-07 07:35:43 +100072enum os_area_boot_flag {
Geoff Levand00a3e2e2006-11-23 00:46:58 +010073 PARAM_BOOT_FLAG_GAME_OS = 0,
74 PARAM_BOOT_FLAG_OTHER_OS = 1,
75};
76
Geoff Levandca942972007-10-07 07:35:43 +100077enum os_area_ctrl_button {
Geoff Levand00a3e2e2006-11-23 00:46:58 +010078 PARAM_CTRL_BUTTON_O_IS_YES = 0,
79 PARAM_CTRL_BUTTON_X_IS_YES = 1,
80};
81
82/**
83 * struct os_area_params - os area params segment.
84 * @boot_flag: User preference of operating system, PARAM_BOOT_FLAG flag.
85 * @num_params: Number of params in this (params) segment.
86 * @rtc_diff: Difference in seconds between 1970 and the ps3 rtc value.
87 * @av_multi_out: User preference of AV output, PARAM_AV_MULTI_OUT flag.
88 * @ctrl_button: User preference of controller button config, PARAM_CTRL_BUTTON
89 * flag.
90 * @static_ip_addr: User preference of static IP address.
91 * @network_mask: User preference of static network mask.
92 * @default_gateway: User preference of static default gateway.
93 * @dns_primary: User preference of static primary dns server.
94 * @dns_secondary: User preference of static secondary dns server.
95 *
Geoff Levandca942972007-10-07 07:35:43 +100096 * The ps3 rtc maintains a read-only value that approximates seconds since
97 * 2000-01-01 00:00:00 UTC.
98 *
Geoff Levand00a3e2e2006-11-23 00:46:58 +010099 * User preference of zero for static_ip_addr means use dhcp.
100 */
101
102struct os_area_params {
103 u32 boot_flag;
104 u32 _reserved_1[3];
105 u32 num_params;
106 u32 _reserved_2[3];
107 /* param 0 */
108 s64 rtc_diff;
109 u8 av_multi_out;
110 u8 ctrl_button;
111 u8 _reserved_3[6];
112 /* param 1 */
113 u8 static_ip_addr[4];
114 u8 network_mask[4];
115 u8 default_gateway[4];
116 u8 _reserved_4[4];
117 /* param 2 */
118 u8 dns_primary[4];
119 u8 dns_secondary[4];
120 u8 _reserved_5[8];
Geoff Levanda8229a92007-01-26 19:07:56 -0800121};
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100122
Geoff Levandec5d2df2007-12-24 04:41:01 +1100123#define OS_AREA_DB_MAGIC_NUM "-db-"
Geoff Levandef2ac632007-10-09 11:07:24 +1000124
125/**
126 * struct os_area_db - Shared flash memory database.
Geoff Levandec5d2df2007-12-24 04:41:01 +1100127 * @magic_num: Always '-db-'.
Geoff Levandef2ac632007-10-09 11:07:24 +1000128 * @version: os_area_db format version number.
129 * @index_64: byte offset of the database id index for 64 bit variables.
130 * @count_64: number of usable 64 bit index entries
131 * @index_32: byte offset of the database id index for 32 bit variables.
132 * @count_32: number of usable 32 bit index entries
133 * @index_16: byte offset of the database id index for 16 bit variables.
134 * @count_16: number of usable 16 bit index entries
135 *
136 * Flash rom storage for exclusive use by guests running in the other os lpar.
137 * The current system configuration allocates 1K (two segments) for other os
138 * use.
139 */
140
141struct os_area_db {
Geoff Levandec5d2df2007-12-24 04:41:01 +1100142 u8 magic_num[4];
Geoff Levandef2ac632007-10-09 11:07:24 +1000143 u16 version;
144 u16 _reserved_1;
145 u16 index_64;
146 u16 count_64;
147 u16 index_32;
148 u16 count_32;
149 u16 index_16;
150 u16 count_16;
151 u32 _reserved_2;
152 u8 _db_data[1000];
153};
154
155/**
156 * enum os_area_db_owner - Data owners.
157 */
158
159enum os_area_db_owner {
160 OS_AREA_DB_OWNER_ANY = -1,
161 OS_AREA_DB_OWNER_NONE = 0,
162 OS_AREA_DB_OWNER_PROTOTYPE = 1,
163 OS_AREA_DB_OWNER_LINUX = 2,
164 OS_AREA_DB_OWNER_PETITBOOT = 3,
165 OS_AREA_DB_OWNER_MAX = 32,
166};
167
168enum os_area_db_key {
169 OS_AREA_DB_KEY_ANY = -1,
170 OS_AREA_DB_KEY_NONE = 0,
171 OS_AREA_DB_KEY_RTC_DIFF = 1,
172 OS_AREA_DB_KEY_VIDEO_MODE = 2,
173 OS_AREA_DB_KEY_MAX = 8,
174};
175
176struct os_area_db_id {
177 int owner;
178 int key;
179};
180
181static const struct os_area_db_id os_area_db_id_empty = {
182 .owner = OS_AREA_DB_OWNER_NONE,
183 .key = OS_AREA_DB_KEY_NONE
184};
185
186static const struct os_area_db_id os_area_db_id_any = {
187 .owner = OS_AREA_DB_OWNER_ANY,
188 .key = OS_AREA_DB_KEY_ANY
189};
190
191static const struct os_area_db_id os_area_db_id_rtc_diff = {
192 .owner = OS_AREA_DB_OWNER_LINUX,
193 .key = OS_AREA_DB_KEY_RTC_DIFF
194};
195
196static const struct os_area_db_id os_area_db_id_video_mode = {
197 .owner = OS_AREA_DB_OWNER_LINUX,
198 .key = OS_AREA_DB_KEY_VIDEO_MODE
199};
200
Geoff Levandca942972007-10-07 07:35:43 +1000201#define SECONDS_FROM_1970_TO_2000 946684800LL
202
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100203/**
Geoff Levand01263e82007-10-07 07:35:44 +1000204 * struct saved_params - Static working copies of data from the PS3 'os area'.
Geoff Levandef2ac632007-10-09 11:07:24 +1000205 *
206 * The order of preference we use for the rtc_diff source:
207 * 1) The database value.
208 * 2) The game os value.
209 * 3) The number of seconds from 1970 to 2000.
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100210 */
211
212struct saved_params {
Geoff Levand7db19422007-10-07 07:35:47 +1000213 unsigned int valid;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100214 s64 rtc_diff;
215 unsigned int av_multi_out;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100216} static saved_params;
217
Geoff Levand7db19422007-10-07 07:35:47 +1000218static struct property property_rtc_diff = {
219 .name = "linux,rtc_diff",
220 .length = sizeof(saved_params.rtc_diff),
221 .value = &saved_params.rtc_diff,
222};
223
224static struct property property_av_multi_out = {
225 .name = "linux,av_multi_out",
226 .length = sizeof(saved_params.av_multi_out),
227 .value = &saved_params.av_multi_out,
228};
229
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000230
231static DEFINE_MUTEX(os_area_flash_mutex);
232
233static const struct ps3_os_area_flash_ops *os_area_flash_ops;
234
235void ps3_os_area_flash_register(const struct ps3_os_area_flash_ops *ops)
236{
237 mutex_lock(&os_area_flash_mutex);
238 os_area_flash_ops = ops;
239 mutex_unlock(&os_area_flash_mutex);
240}
241EXPORT_SYMBOL_GPL(ps3_os_area_flash_register);
242
243static ssize_t os_area_flash_read(void *buf, size_t count, loff_t pos)
244{
245 ssize_t res = -ENODEV;
246
247 mutex_lock(&os_area_flash_mutex);
248 if (os_area_flash_ops)
249 res = os_area_flash_ops->read(buf, count, pos);
250 mutex_unlock(&os_area_flash_mutex);
251
252 return res;
253}
254
255static ssize_t os_area_flash_write(const void *buf, size_t count, loff_t pos)
256{
257 ssize_t res = -ENODEV;
258
259 mutex_lock(&os_area_flash_mutex);
260 if (os_area_flash_ops)
261 res = os_area_flash_ops->write(buf, count, pos);
262 mutex_unlock(&os_area_flash_mutex);
263
264 return res;
265}
266
267
Geoff Levand7db19422007-10-07 07:35:47 +1000268/**
269 * os_area_set_property - Add or overwrite a saved_params value to the device tree.
270 *
271 * Overwrites an existing property.
272 */
273
274static void os_area_set_property(struct device_node *node,
275 struct property *prop)
276{
277 int result;
278 struct property *tmp = of_find_property(node, prop->name, NULL);
279
280 if (tmp) {
281 pr_debug("%s:%d found %s\n", __func__, __LINE__, prop->name);
282 prom_remove_property(node, tmp);
283 }
284
285 result = prom_add_property(node, prop);
286
287 if (result)
288 pr_debug("%s:%d prom_set_property failed\n", __func__,
289 __LINE__);
290}
291
292/**
293 * os_area_get_property - Get a saved_params value from the device tree.
294 *
295 */
296
297static void __init os_area_get_property(struct device_node *node,
298 struct property *prop)
299{
300 const struct property *tmp = of_find_property(node, prop->name, NULL);
301
302 if (tmp) {
303 BUG_ON(prop->length != tmp->length);
304 memcpy(prop->value, tmp->value, prop->length);
305 } else
306 pr_debug("%s:%d not found %s\n", __func__, __LINE__,
307 prop->name);
308}
309
Geoff Levandec5d2df2007-12-24 04:41:01 +1100310static void dump_field(char *s, const u8 *field, int size_of_field)
311{
312#if defined(DEBUG)
313 int i;
314
315 for (i = 0; i < size_of_field; i++)
316 s[i] = isprint(field[i]) ? field[i] : '.';
317 s[i] = 0;
318#endif
319}
320
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100321#define dump_header(_a) _dump_header(_a, __func__, __LINE__)
Geert Uytterhoeven670ad352007-06-16 07:19:04 +1000322static void _dump_header(const struct os_area_header *h, const char *func,
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100323 int line)
324{
Geoff Levandec5d2df2007-12-24 04:41:01 +1100325 char str[sizeof(h->magic_num) + 1];
326
327 dump_field(str, h->magic_num, sizeof(h->magic_num));
Geoff Levandef2ac632007-10-09 11:07:24 +1000328 pr_debug("%s:%d: h.magic_num: '%s'\n", func, line,
Geoff Levandec5d2df2007-12-24 04:41:01 +1100329 str);
Geoff Levandef2ac632007-10-09 11:07:24 +1000330 pr_debug("%s:%d: h.hdr_version: %u\n", func, line,
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100331 h->hdr_version);
Geoff Levandef2ac632007-10-09 11:07:24 +1000332 pr_debug("%s:%d: h.db_area_offset: %u\n", func, line,
333 h->db_area_offset);
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100334 pr_debug("%s:%d: h.ldr_area_offset: %u\n", func, line,
335 h->ldr_area_offset);
Geoff Levandef2ac632007-10-09 11:07:24 +1000336 pr_debug("%s:%d: h.ldr_format: %u\n", func, line,
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100337 h->ldr_format);
Geoff Levandef2ac632007-10-09 11:07:24 +1000338 pr_debug("%s:%d: h.ldr_size: %xh\n", func, line,
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100339 h->ldr_size);
340}
341
342#define dump_params(_a) _dump_params(_a, __func__, __LINE__)
Geert Uytterhoeven670ad352007-06-16 07:19:04 +1000343static void _dump_params(const struct os_area_params *p, const char *func,
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100344 int line)
345{
346 pr_debug("%s:%d: p.boot_flag: %u\n", func, line, p->boot_flag);
347 pr_debug("%s:%d: p.num_params: %u\n", func, line, p->num_params);
Stephen Rothwell5c949072009-01-13 20:02:39 +0000348 pr_debug("%s:%d: p.rtc_diff %lld\n", func, line, p->rtc_diff);
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100349 pr_debug("%s:%d: p.av_multi_out %u\n", func, line, p->av_multi_out);
350 pr_debug("%s:%d: p.ctrl_button: %u\n", func, line, p->ctrl_button);
351 pr_debug("%s:%d: p.static_ip_addr: %u.%u.%u.%u\n", func, line,
352 p->static_ip_addr[0], p->static_ip_addr[1],
353 p->static_ip_addr[2], p->static_ip_addr[3]);
354 pr_debug("%s:%d: p.network_mask: %u.%u.%u.%u\n", func, line,
355 p->network_mask[0], p->network_mask[1],
356 p->network_mask[2], p->network_mask[3]);
357 pr_debug("%s:%d: p.default_gateway: %u.%u.%u.%u\n", func, line,
358 p->default_gateway[0], p->default_gateway[1],
359 p->default_gateway[2], p->default_gateway[3]);
360 pr_debug("%s:%d: p.dns_primary: %u.%u.%u.%u\n", func, line,
361 p->dns_primary[0], p->dns_primary[1],
362 p->dns_primary[2], p->dns_primary[3]);
363 pr_debug("%s:%d: p.dns_secondary: %u.%u.%u.%u\n", func, line,
364 p->dns_secondary[0], p->dns_secondary[1],
365 p->dns_secondary[2], p->dns_secondary[3]);
366}
367
Geoff Levandef2ac632007-10-09 11:07:24 +1000368static int verify_header(const struct os_area_header *header)
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100369{
Geoff Levandec5d2df2007-12-24 04:41:01 +1100370 if (memcmp(header->magic_num, OS_AREA_HEADER_MAGIC_NUM,
371 sizeof(header->magic_num))) {
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100372 pr_debug("%s:%d magic_num failed\n", __func__, __LINE__);
373 return -1;
374 }
375
376 if (header->hdr_version < 1) {
377 pr_debug("%s:%d hdr_version failed\n", __func__, __LINE__);
378 return -1;
379 }
380
Geoff Levandef2ac632007-10-09 11:07:24 +1000381 if (header->db_area_offset > header->ldr_area_offset) {
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100382 pr_debug("%s:%d offsets failed\n", __func__, __LINE__);
383 return -1;
384 }
385
386 return 0;
387}
388
Geoff Levandef2ac632007-10-09 11:07:24 +1000389static int db_verify(const struct os_area_db *db)
390{
Geoff Levandec5d2df2007-12-24 04:41:01 +1100391 if (memcmp(db->magic_num, OS_AREA_DB_MAGIC_NUM,
392 sizeof(db->magic_num))) {
Geoff Levandef2ac632007-10-09 11:07:24 +1000393 pr_debug("%s:%d magic_num failed\n", __func__, __LINE__);
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000394 return -EINVAL;
Geoff Levandef2ac632007-10-09 11:07:24 +1000395 }
396
397 if (db->version != 1) {
398 pr_debug("%s:%d version failed\n", __func__, __LINE__);
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000399 return -EINVAL;
Geoff Levandef2ac632007-10-09 11:07:24 +1000400 }
401
402 return 0;
403}
404
405struct db_index {
406 uint8_t owner:5;
407 uint8_t key:3;
408};
409
410struct db_iterator {
411 const struct os_area_db *db;
412 struct os_area_db_id match_id;
413 struct db_index *idx;
414 struct db_index *last_idx;
415 union {
416 uint64_t *value_64;
417 uint32_t *value_32;
418 uint16_t *value_16;
419 };
420};
421
422static unsigned int db_align_up(unsigned int val, unsigned int size)
423{
424 return (val + (size - 1)) & (~(size - 1));
425}
426
427/**
428 * db_for_each_64 - Iterator for 64 bit entries.
429 *
430 * A NULL value for id can be used to match all entries.
431 * OS_AREA_DB_OWNER_ANY and OS_AREA_DB_KEY_ANY can be used to match all.
432 */
433
434static int db_for_each_64(const struct os_area_db *db,
435 const struct os_area_db_id *match_id, struct db_iterator *i)
436{
437next:
438 if (!i->db) {
439 i->db = db;
440 i->match_id = match_id ? *match_id : os_area_db_id_any;
441 i->idx = (void *)db + db->index_64;
442 i->last_idx = i->idx + db->count_64;
443 i->value_64 = (void *)db + db->index_64
444 + db_align_up(db->count_64, 8);
445 } else {
446 i->idx++;
447 i->value_64++;
448 }
449
450 if (i->idx >= i->last_idx) {
451 pr_debug("%s:%d: reached end\n", __func__, __LINE__);
452 return 0;
453 }
454
455 if (i->match_id.owner != OS_AREA_DB_OWNER_ANY
456 && i->match_id.owner != (int)i->idx->owner)
457 goto next;
458 if (i->match_id.key != OS_AREA_DB_KEY_ANY
459 && i->match_id.key != (int)i->idx->key)
460 goto next;
461
462 return 1;
463}
464
465static int db_delete_64(struct os_area_db *db, const struct os_area_db_id *id)
466{
467 struct db_iterator i;
468
469 for (i.db = NULL; db_for_each_64(db, id, &i); ) {
470
471 pr_debug("%s:%d: got (%d:%d) %llxh\n", __func__, __LINE__,
472 i.idx->owner, i.idx->key,
473 (unsigned long long)*i.value_64);
474
475 i.idx->owner = 0;
476 i.idx->key = 0;
477 *i.value_64 = 0;
478 }
479 return 0;
480}
481
482static int db_set_64(struct os_area_db *db, const struct os_area_db_id *id,
483 uint64_t value)
484{
485 struct db_iterator i;
486
487 pr_debug("%s:%d: (%d:%d) <= %llxh\n", __func__, __LINE__,
488 id->owner, id->key, (unsigned long long)value);
489
490 if (!id->owner || id->owner == OS_AREA_DB_OWNER_ANY
491 || id->key == OS_AREA_DB_KEY_ANY) {
492 pr_debug("%s:%d: bad id: (%d:%d)\n", __func__,
493 __LINE__, id->owner, id->key);
494 return -1;
495 }
496
497 db_delete_64(db, id);
498
499 i.db = NULL;
500 if (db_for_each_64(db, &os_area_db_id_empty, &i)) {
501
502 pr_debug("%s:%d: got (%d:%d) %llxh\n", __func__, __LINE__,
503 i.idx->owner, i.idx->key,
504 (unsigned long long)*i.value_64);
505
506 i.idx->owner = id->owner;
507 i.idx->key = id->key;
508 *i.value_64 = value;
509
510 pr_debug("%s:%d: set (%d:%d) <= %llxh\n", __func__, __LINE__,
511 i.idx->owner, i.idx->key,
512 (unsigned long long)*i.value_64);
513 return 0;
514 }
515 pr_debug("%s:%d: database full.\n",
516 __func__, __LINE__);
517 return -1;
518}
519
520static int db_get_64(const struct os_area_db *db,
521 const struct os_area_db_id *id, uint64_t *value)
522{
523 struct db_iterator i;
524
525 i.db = NULL;
526 if (db_for_each_64(db, id, &i)) {
527 *value = *i.value_64;
528 pr_debug("%s:%d: found %lld\n", __func__, __LINE__,
529 (long long int)*i.value_64);
530 return 0;
531 }
532 pr_debug("%s:%d: not found\n", __func__, __LINE__);
533 return -1;
534}
535
536static int db_get_rtc_diff(const struct os_area_db *db, int64_t *rtc_diff)
537{
538 return db_get_64(db, &os_area_db_id_rtc_diff, (uint64_t*)rtc_diff);
539}
540
541#define dump_db(a) _dump_db(a, __func__, __LINE__)
542static void _dump_db(const struct os_area_db *db, const char *func,
543 int line)
544{
Geoff Levandec5d2df2007-12-24 04:41:01 +1100545 char str[sizeof(db->magic_num) + 1];
546
547 dump_field(str, db->magic_num, sizeof(db->magic_num));
Geoff Levandef2ac632007-10-09 11:07:24 +1000548 pr_debug("%s:%d: db.magic_num: '%s'\n", func, line,
Geoff Levandec5d2df2007-12-24 04:41:01 +1100549 str);
Geoff Levandef2ac632007-10-09 11:07:24 +1000550 pr_debug("%s:%d: db.version: %u\n", func, line,
551 db->version);
552 pr_debug("%s:%d: db.index_64: %u\n", func, line,
553 db->index_64);
554 pr_debug("%s:%d: db.count_64: %u\n", func, line,
555 db->count_64);
556 pr_debug("%s:%d: db.index_32: %u\n", func, line,
557 db->index_32);
558 pr_debug("%s:%d: db.count_32: %u\n", func, line,
559 db->count_32);
560 pr_debug("%s:%d: db.index_16: %u\n", func, line,
561 db->index_16);
562 pr_debug("%s:%d: db.count_16: %u\n", func, line,
563 db->count_16);
564}
565
566static void os_area_db_init(struct os_area_db *db)
567{
568 enum {
569 HEADER_SIZE = offsetof(struct os_area_db, _db_data),
570 INDEX_64_COUNT = 64,
571 VALUES_64_COUNT = 57,
572 INDEX_32_COUNT = 64,
573 VALUES_32_COUNT = 57,
574 INDEX_16_COUNT = 64,
575 VALUES_16_COUNT = 57,
576 };
577
578 memset(db, 0, sizeof(struct os_area_db));
579
Geoff Levandec5d2df2007-12-24 04:41:01 +1100580 memcpy(db->magic_num, OS_AREA_DB_MAGIC_NUM, sizeof(db->magic_num));
Geoff Levandef2ac632007-10-09 11:07:24 +1000581 db->version = 1;
582 db->index_64 = HEADER_SIZE;
583 db->count_64 = VALUES_64_COUNT;
584 db->index_32 = HEADER_SIZE
585 + INDEX_64_COUNT * sizeof(struct db_index)
586 + VALUES_64_COUNT * sizeof(u64);
587 db->count_32 = VALUES_32_COUNT;
588 db->index_16 = HEADER_SIZE
589 + INDEX_64_COUNT * sizeof(struct db_index)
590 + VALUES_64_COUNT * sizeof(u64)
591 + INDEX_32_COUNT * sizeof(struct db_index)
592 + VALUES_32_COUNT * sizeof(u32);
593 db->count_16 = VALUES_16_COUNT;
594
595 /* Rules to check db layout. */
596
597 BUILD_BUG_ON(sizeof(struct db_index) != 1);
598 BUILD_BUG_ON(sizeof(struct os_area_db) != 2 * OS_AREA_SEGMENT_SIZE);
599 BUILD_BUG_ON(INDEX_64_COUNT & 0x7);
600 BUILD_BUG_ON(VALUES_64_COUNT > INDEX_64_COUNT);
601 BUILD_BUG_ON(INDEX_32_COUNT & 0x7);
602 BUILD_BUG_ON(VALUES_32_COUNT > INDEX_32_COUNT);
603 BUILD_BUG_ON(INDEX_16_COUNT & 0x7);
604 BUILD_BUG_ON(VALUES_16_COUNT > INDEX_16_COUNT);
605 BUILD_BUG_ON(HEADER_SIZE
606 + INDEX_64_COUNT * sizeof(struct db_index)
607 + VALUES_64_COUNT * sizeof(u64)
608 + INDEX_32_COUNT * sizeof(struct db_index)
609 + VALUES_32_COUNT * sizeof(u32)
610 + INDEX_16_COUNT * sizeof(struct db_index)
611 + VALUES_16_COUNT * sizeof(u16)
612 > sizeof(struct os_area_db));
613}
614
615/**
616 * update_flash_db - Helper for os_area_queue_work_handler.
617 *
618 */
619
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000620static int update_flash_db(void)
Geoff Levandef2ac632007-10-09 11:07:24 +1000621{
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000622 const unsigned int buf_len = 8 * OS_AREA_SEGMENT_SIZE;
623 struct os_area_header *header;
Geoff Levandef2ac632007-10-09 11:07:24 +1000624 ssize_t count;
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000625 int error;
626 loff_t pos;
Geoff Levandef2ac632007-10-09 11:07:24 +1000627 struct os_area_db* db;
628
629 /* Read in header and db from flash. */
630
Geoff Levandef2ac632007-10-09 11:07:24 +1000631 header = kmalloc(buf_len, GFP_KERNEL);
Geoff Levandef2ac632007-10-09 11:07:24 +1000632 if (!header) {
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000633 pr_debug("%s: kmalloc failed\n", __func__);
634 return -ENOMEM;
Geoff Levandef2ac632007-10-09 11:07:24 +1000635 }
636
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000637 count = os_area_flash_read(header, buf_len, 0);
638 if (count < 0) {
639 pr_debug("%s: os_area_flash_read failed %zd\n", __func__,
640 count);
641 error = count;
642 goto fail;
Geoff Levandef2ac632007-10-09 11:07:24 +1000643 }
644
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000645 pos = header->db_area_offset * OS_AREA_SEGMENT_SIZE;
646 if (count < OS_AREA_SEGMENT_SIZE || verify_header(header) ||
647 count < pos) {
648 pr_debug("%s: verify_header failed\n", __func__);
Geoff Levandef2ac632007-10-09 11:07:24 +1000649 dump_header(header);
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000650 error = -EINVAL;
651 goto fail;
Geoff Levandef2ac632007-10-09 11:07:24 +1000652 }
653
654 /* Now got a good db offset and some maybe good db data. */
655
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000656 db = (void *)header + pos;
Geoff Levandef2ac632007-10-09 11:07:24 +1000657
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000658 error = db_verify(db);
659 if (error) {
660 pr_notice("%s: Verify of flash database failed, formatting.\n",
661 __func__);
Geoff Levandef2ac632007-10-09 11:07:24 +1000662 dump_db(db);
663 os_area_db_init(db);
664 }
665
666 /* Now got good db data. */
667
668 db_set_64(db, &os_area_db_id_rtc_diff, saved_params.rtc_diff);
669
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000670 count = os_area_flash_write(db, sizeof(struct os_area_db), pos);
Geoff Levandef2ac632007-10-09 11:07:24 +1000671 if (count < sizeof(struct os_area_db)) {
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000672 pr_debug("%s: os_area_flash_write failed %zd\n", __func__,
673 count);
674 error = count < 0 ? count : -EIO;
Geoff Levandef2ac632007-10-09 11:07:24 +1000675 }
676
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000677fail:
Geoff Levandef2ac632007-10-09 11:07:24 +1000678 kfree(header);
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000679 return error;
Geoff Levandef2ac632007-10-09 11:07:24 +1000680}
681
Geoff Levand01263e82007-10-07 07:35:44 +1000682/**
Geoff Levand418ef202007-10-07 07:35:45 +1000683 * os_area_queue_work_handler - Asynchronous write handler.
684 *
685 * An asynchronous write for flash memory and the device tree. Do not
686 * call directly, use os_area_queue_work().
687 */
688
689static void os_area_queue_work_handler(struct work_struct *work)
690{
Geoff Levand7db19422007-10-07 07:35:47 +1000691 struct device_node *node;
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000692 int error;
Geoff Levand7db19422007-10-07 07:35:47 +1000693
Geoff Levand418ef202007-10-07 07:35:45 +1000694 pr_debug(" -> %s:%d\n", __func__, __LINE__);
695
Geoff Levand7db19422007-10-07 07:35:47 +1000696 node = of_find_node_by_path("/");
Geoff Levand7db19422007-10-07 07:35:47 +1000697 if (node) {
698 os_area_set_property(node, &property_rtc_diff);
699 of_node_put(node);
700 } else
701 pr_debug("%s:%d of_find_node_by_path failed\n",
702 __func__, __LINE__);
703
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000704 error = update_flash_db();
705 if (error)
706 pr_warning("%s: Could not update FLASH ROM\n", __func__);
707
Geoff Levand418ef202007-10-07 07:35:45 +1000708 pr_debug(" <- %s:%d\n", __func__, __LINE__);
709}
710
711static void os_area_queue_work(void)
712{
713 static DECLARE_WORK(q, os_area_queue_work_handler);
714
715 wmb();
716 schedule_work(&q);
717}
718
719/**
Geoff Levand01263e82007-10-07 07:35:44 +1000720 * ps3_os_area_save_params - Copy data from os area mirror to @saved_params.
721 *
Geoff Levandef2ac632007-10-09 11:07:24 +1000722 * For the convenience of the guest the HV makes a copy of the os area in
Geoff Levand01263e82007-10-07 07:35:44 +1000723 * flash to a high address in the boot memory region and then puts that RAM
Geoff Levandef2ac632007-10-09 11:07:24 +1000724 * address and the byte count into the repository for retrieval by the guest.
Geoff Levand01263e82007-10-07 07:35:44 +1000725 * We copy the data we want into a static variable and allow the memory setup
726 * by the HV to be claimed by the lmb manager.
Geoff Levandef2ac632007-10-09 11:07:24 +1000727 *
728 * The os area mirror will not be available to a second stage kernel, and
729 * the header verify will fail. In this case, the saved_params values will
730 * be set from flash memory or the passed in device tree in ps3_os_area_init().
Geoff Levand01263e82007-10-07 07:35:44 +1000731 */
732
733void __init ps3_os_area_save_params(void)
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100734{
735 int result;
736 u64 lpar_addr;
737 unsigned int size;
738 struct os_area_header *header;
739 struct os_area_params *params;
Geoff Levandef2ac632007-10-09 11:07:24 +1000740 struct os_area_db *db;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100741
Geoff Levand01263e82007-10-07 07:35:44 +1000742 pr_debug(" -> %s:%d\n", __func__, __LINE__);
743
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100744 result = ps3_repository_read_boot_dat_info(&lpar_addr, &size);
745
746 if (result) {
747 pr_debug("%s:%d ps3_repository_read_boot_dat_info failed\n",
748 __func__, __LINE__);
Geoff Levand01263e82007-10-07 07:35:44 +1000749 return;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100750 }
751
752 header = (struct os_area_header *)__va(lpar_addr);
Geoff Levandca942972007-10-07 07:35:43 +1000753 params = (struct os_area_params *)__va(lpar_addr
754 + OS_AREA_SEGMENT_SIZE);
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100755
756 result = verify_header(header);
757
758 if (result) {
Geoff Levand7db19422007-10-07 07:35:47 +1000759 /* Second stage kernels exit here. */
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100760 pr_debug("%s:%d verify_header failed\n", __func__, __LINE__);
761 dump_header(header);
Geoff Levand01263e82007-10-07 07:35:44 +1000762 return;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100763 }
764
Geoff Levandef2ac632007-10-09 11:07:24 +1000765 db = (struct os_area_db *)__va(lpar_addr
766 + header->db_area_offset * OS_AREA_SEGMENT_SIZE);
767
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100768 dump_header(header);
769 dump_params(params);
Geoff Levandef2ac632007-10-09 11:07:24 +1000770 dump_db(db);
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100771
Geoff Levandef2ac632007-10-09 11:07:24 +1000772 result = db_verify(db) || db_get_rtc_diff(db, &saved_params.rtc_diff);
773 if (result)
774 saved_params.rtc_diff = params->rtc_diff ? params->rtc_diff
775 : SECONDS_FROM_1970_TO_2000;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100776 saved_params.av_multi_out = params->av_multi_out;
Geoff Levand7db19422007-10-07 07:35:47 +1000777 saved_params.valid = 1;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100778
Geoff Levand01263e82007-10-07 07:35:44 +1000779 memset(header, 0, sizeof(*header));
780
781 pr_debug(" <- %s:%d\n", __func__, __LINE__);
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100782}
783
784/**
Geoff Levand7db19422007-10-07 07:35:47 +1000785 * ps3_os_area_init - Setup os area device tree properties as needed.
786 */
787
788void __init ps3_os_area_init(void)
789{
790 struct device_node *node;
791
792 pr_debug(" -> %s:%d\n", __func__, __LINE__);
793
794 node = of_find_node_by_path("/");
795
796 if (!saved_params.valid && node) {
797 /* Second stage kernels should have a dt entry. */
798 os_area_get_property(node, &property_rtc_diff);
799 os_area_get_property(node, &property_av_multi_out);
800 }
801
802 if(!saved_params.rtc_diff)
803 saved_params.rtc_diff = SECONDS_FROM_1970_TO_2000;
804
805 if (node) {
806 os_area_set_property(node, &property_rtc_diff);
807 os_area_set_property(node, &property_av_multi_out);
808 of_node_put(node);
809 } else
810 pr_debug("%s:%d of_find_node_by_path failed\n",
811 __func__, __LINE__);
812
813 pr_debug(" <- %s:%d\n", __func__, __LINE__);
814}
815
816/**
Geoff Levandd7b98e32007-10-07 07:35:46 +1000817 * ps3_os_area_get_rtc_diff - Returns the rtc diff value.
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100818 */
819
Geoff Levandd7b98e32007-10-07 07:35:46 +1000820u64 ps3_os_area_get_rtc_diff(void)
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100821{
Geoff Levand7db19422007-10-07 07:35:47 +1000822 return saved_params.rtc_diff;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100823}
Geert Uytterhoeven47cb9962009-06-10 04:39:05 +0000824EXPORT_SYMBOL_GPL(ps3_os_area_get_rtc_diff);
Geert Uytterhoeven098e2742007-01-26 19:08:24 -0800825
826/**
Geoff Levandd7b98e32007-10-07 07:35:46 +1000827 * ps3_os_area_set_rtc_diff - Set the rtc diff value.
828 *
829 * An asynchronous write is needed to support writing updates from
830 * the timer interrupt context.
831 */
832
833void ps3_os_area_set_rtc_diff(u64 rtc_diff)
834{
835 if (saved_params.rtc_diff != rtc_diff) {
836 saved_params.rtc_diff = rtc_diff;
837 os_area_queue_work();
838 }
839}
Geert Uytterhoeven47cb9962009-06-10 04:39:05 +0000840EXPORT_SYMBOL_GPL(ps3_os_area_set_rtc_diff);
Geoff Levandd7b98e32007-10-07 07:35:46 +1000841
842/**
Geert Uytterhoeven098e2742007-01-26 19:08:24 -0800843 * ps3_os_area_get_av_multi_out - Returns the default video mode.
844 */
845
846enum ps3_param_av_multi_out ps3_os_area_get_av_multi_out(void)
847{
848 return saved_params.av_multi_out;
849}
850EXPORT_SYMBOL_GPL(ps3_os_area_get_av_multi_out);