Skip to content

Commit 183d020

Browse files
ozbenhpaulusmack
authored andcommitted
[PATCH] ppc64: SMU partition recovery
This patch adds the ability to the SMU driver to recover missing calibration partitions from the SMU chip itself. It also adds some dynamic mecanism to /proc/device-tree so that new properties are visible to userland. Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org> Signed-off-by: Paul Mackerras <paulus@samba.org>
1 parent 4350147 commit 183d020

10 files changed

Lines changed: 381 additions & 57 deletions

File tree

‎arch/powerpc/kernel/prom.c‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,14 +1974,29 @@ EXPORT_SYMBOL(get_property);
19741974
/*
19751975
* Add a property to a node
19761976
*/
1977-
void prom_add_property(struct device_node* np, struct property* prop)
1977+
int prom_add_property(struct device_node* np, struct property* prop)
19781978
{
1979-
struct property **next = &np->properties;
1979+
struct property **next;
19801980

19811981
prop->next = NULL;
1982-
while (*next)
1982+
write_lock(&devtree_lock);
1983+
next = &np->properties;
1984+
while (*next) {
1985+
if (strcmp(prop->name, (*next)->name) == 0) {
1986+
/* duplicate ! don't insert it */
1987+
write_unlock(&devtree_lock);
1988+
return -1;
1989+
}
19831990
next = &(*next)->next;
1991+
}
19841992
*next = prop;
1993+
write_unlock(&devtree_lock);
1994+
1995+
/* try to add to proc as well if it was initialized */
1996+
if (np->pde)
1997+
proc_device_tree_add_prop(np->pde, prop);
1998+
1999+
return 0;
19852000
}
19862001

19872002
/* I quickly hacked that one, check against spec ! */

‎arch/ppc/syslib/prom.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ get_property(struct device_node *np, const char *name, int *lenp)
11651165
/*
11661166
* Add a property to a node
11671167
*/
1168-
void
1168+
int
11691169
prom_add_property(struct device_node* np, struct property* prop)
11701170
{
11711171
struct property **next = &np->properties;
@@ -1174,6 +1174,8 @@ prom_add_property(struct device_node* np, struct property* prop)
11741174
while (*next)
11751175
next = &(*next)->next;
11761176
*next = prop;
1177+
1178+
return 0;
11771179
}
11781180

11791181
/* I quickly hacked that one, check against spec ! */

‎arch/ppc64/kernel/prom.c‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/initrd.h>
3232
#include <linux/bitops.h>
3333
#include <linux/module.h>
34+
#include <linux/module.h>
3435

3536
#include <asm/prom.h>
3637
#include <asm/rtas.h>
@@ -1865,17 +1866,32 @@ get_property(struct device_node *np, const char *name, int *lenp)
18651866
EXPORT_SYMBOL(get_property);
18661867

18671868
/*
1868-
* Add a property to a node
1869+
* Add a property to a node.
18691870
*/
1870-
void
1871+
int
18711872
prom_add_property(struct device_node* np, struct property* prop)
18721873
{
1873-
struct property **next = &np->properties;
1874+
struct property **next;
18741875

18751876
prop->next = NULL;
1876-
while (*next)
1877+
write_lock(&devtree_lock);
1878+
next = &np->properties;
1879+
while (*next) {
1880+
if (strcmp(prop->name, (*next)->name) == 0) {
1881+
/* duplicate ! don't insert it */
1882+
write_unlock(&devtree_lock);
1883+
return -1;
1884+
}
18771885
next = &(*next)->next;
1886+
}
18781887
*next = prop;
1888+
write_unlock(&devtree_lock);
1889+
1890+
/* try to add to proc as well if it was initialized */
1891+
if (np->pde)
1892+
proc_device_tree_add_prop(np->pde, prop);
1893+
1894+
return 0;
18791895
}
18801896

18811897
#if 0

‎drivers/macintosh/smu.c‎

Lines changed: 156 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@
4747
#include <asm/uaccess.h>
4848
#include <asm/of_device.h>
4949

50-
#define VERSION "0.6"
50+
#define VERSION "0.7"
5151
#define AUTHOR "(c) 2005 Benjamin Herrenschmidt, IBM Corp."
5252

5353
#undef DEBUG_SMU
5454

5555
#ifdef DEBUG_SMU
56-
#define DPRINTK(fmt, args...) do { printk(KERN_DEBUG fmt , ##args); } while (0)
56+
#define DPRINTK(fmt, args...) do { udbg_printf(KERN_DEBUG fmt , ##args); } while (0)
5757
#else
5858
#define DPRINTK(fmt, args...) do { } while (0)
5959
#endif
@@ -92,7 +92,7 @@ struct smu_device {
9292
* for now, just hard code that
9393
*/
9494
static struct smu_device *smu;
95-
95+
static DECLARE_MUTEX(smu_part_access);
9696

9797
/*
9898
* SMU driver low level stuff
@@ -113,9 +113,11 @@ static void smu_start_cmd(void)
113113

114114
DPRINTK("SMU: starting cmd %x, %d bytes data\n", cmd->cmd,
115115
cmd->data_len);
116-
DPRINTK("SMU: data buffer: %02x %02x %02x %02x ...\n",
116+
DPRINTK("SMU: data buffer: %02x %02x %02x %02x %02x %02x %02x %02x\n",
117117
((u8 *)cmd->data_buf)[0], ((u8 *)cmd->data_buf)[1],
118-
((u8 *)cmd->data_buf)[2], ((u8 *)cmd->data_buf)[3]);
118+
((u8 *)cmd->data_buf)[2], ((u8 *)cmd->data_buf)[3],
119+
((u8 *)cmd->data_buf)[4], ((u8 *)cmd->data_buf)[5],
120+
((u8 *)cmd->data_buf)[6], ((u8 *)cmd->data_buf)[7]);
119121

120122
/* Fill the SMU command buffer */
121123
smu->cmd_buf->cmd = cmd->cmd;
@@ -440,7 +442,7 @@ int smu_present(void)
440442
EXPORT_SYMBOL(smu_present);
441443

442444

443-
int smu_init (void)
445+
int __init smu_init (void)
444446
{
445447
struct device_node *np;
446448
u32 *data;
@@ -845,16 +847,154 @@ int smu_queue_i2c(struct smu_i2c_cmd *cmd)
845847
return 0;
846848
}
847849

848-
struct smu_sdbp_header *smu_get_sdb_partition(int id, unsigned int *size)
850+
/*
851+
* Handling of "partitions"
852+
*/
853+
854+
static int smu_read_datablock(u8 *dest, unsigned int addr, unsigned int len)
855+
{
856+
DECLARE_COMPLETION(comp);
857+
unsigned int chunk;
858+
struct smu_cmd cmd;
859+
int rc;
860+
u8 params[8];
861+
862+
/* We currently use a chunk size of 0xe. We could check the
863+
* SMU firmware version and use bigger sizes though
864+
*/
865+
chunk = 0xe;
866+
867+
while (len) {
868+
unsigned int clen = min(len, chunk);
869+
870+
cmd.cmd = SMU_CMD_MISC_ee_COMMAND;
871+
cmd.data_len = 7;
872+
cmd.data_buf = params;
873+
cmd.reply_len = chunk;
874+
cmd.reply_buf = dest;
875+
cmd.done = smu_done_complete;
876+
cmd.misc = &comp;
877+
params[0] = SMU_CMD_MISC_ee_GET_DATABLOCK_REC;
878+
params[1] = 0x4;
879+
*((u32 *)&params[2]) = addr;
880+
params[6] = clen;
881+
882+
rc = smu_queue_cmd(&cmd);
883+
if (rc)
884+
return rc;
885+
wait_for_completion(&comp);
886+
if (cmd.status != 0)
887+
return rc;
888+
if (cmd.reply_len != clen) {
889+
printk(KERN_DEBUG "SMU: short read in "
890+
"smu_read_datablock, got: %d, want: %d\n",
891+
cmd.reply_len, clen);
892+
return -EIO;
893+
}
894+
len -= clen;
895+
addr += clen;
896+
dest += clen;
897+
}
898+
return 0;
899+
}
900+
901+
static struct smu_sdbp_header *smu_create_sdb_partition(int id)
902+
{
903+
DECLARE_COMPLETION(comp);
904+
struct smu_simple_cmd cmd;
905+
unsigned int addr, len, tlen;
906+
struct smu_sdbp_header *hdr;
907+
struct property *prop;
908+
909+
/* First query the partition info */
910+
smu_queue_simple(&cmd, SMU_CMD_PARTITION_COMMAND, 2,
911+
smu_done_complete, &comp,
912+
SMU_CMD_PARTITION_LATEST, id);
913+
wait_for_completion(&comp);
914+
915+
/* Partition doesn't exist (or other error) */
916+
if (cmd.cmd.status != 0 || cmd.cmd.reply_len != 6)
917+
return NULL;
918+
919+
/* Fetch address and length from reply */
920+
addr = *((u16 *)cmd.buffer);
921+
len = cmd.buffer[3] << 2;
922+
/* Calucluate total length to allocate, including the 17 bytes
923+
* for "sdb-partition-XX" that we append at the end of the buffer
924+
*/
925+
tlen = sizeof(struct property) + len + 18;
926+
927+
prop = kcalloc(tlen, 1, GFP_KERNEL);
928+
if (prop == NULL)
929+
return NULL;
930+
hdr = (struct smu_sdbp_header *)(prop + 1);
931+
prop->name = ((char *)prop) + tlen - 18;
932+
sprintf(prop->name, "sdb-partition-%02x", id);
933+
prop->length = len;
934+
prop->value = (unsigned char *)hdr;
935+
prop->next = NULL;
936+
937+
/* Read the datablock */
938+
if (smu_read_datablock((u8 *)hdr, addr, len)) {
939+
printk(KERN_DEBUG "SMU: datablock read failed while reading "
940+
"partition %02x !\n", id);
941+
goto failure;
942+
}
943+
944+
/* Got it, check a few things and create the property */
945+
if (hdr->id != id) {
946+
printk(KERN_DEBUG "SMU: Reading partition %02x and got "
947+
"%02x !\n", id, hdr->id);
948+
goto failure;
949+
}
950+
if (prom_add_property(smu->of_node, prop)) {
951+
printk(KERN_DEBUG "SMU: Failed creating sdb-partition-%02x "
952+
"property !\n", id);
953+
goto failure;
954+
}
955+
956+
return hdr;
957+
failure:
958+
kfree(prop);
959+
return NULL;
960+
}
961+
962+
/* Note: Only allowed to return error code in pointers (using ERR_PTR)
963+
* when interruptible is 1
964+
*/
965+
struct smu_sdbp_header *__smu_get_sdb_partition(int id, unsigned int *size,
966+
int interruptible)
849967
{
850968
char pname[32];
969+
struct smu_sdbp_header *part;
851970

852971
if (!smu)
853972
return NULL;
854973

855974
sprintf(pname, "sdb-partition-%02x", id);
856-
return (struct smu_sdbp_header *)get_property(smu->of_node,
975+
976+
if (interruptible) {
977+
int rc;
978+
rc = down_interruptible(&smu_part_access);
979+
if (rc)
980+
return ERR_PTR(rc);
981+
} else
982+
down(&smu_part_access);
983+
984+
part = (struct smu_sdbp_header *)get_property(smu->of_node,
857985
pname, size);
986+
if (part == NULL) {
987+
part = smu_create_sdb_partition(id);
988+
if (part != NULL && size)
989+
*size = part->len << 2;
990+
}
991+
up(&smu_part_access);
992+
return part;
993+
}
994+
995+
struct smu_sdbp_header *smu_get_sdb_partition(int id, unsigned int *size)
996+
{
997+
return __smu_get_sdb_partition(id, size, 0);
858998
}
859999
EXPORT_SYMBOL(smu_get_sdb_partition);
8601000

@@ -930,6 +1070,14 @@ static ssize_t smu_write(struct file *file, const char __user *buf,
9301070
else if (hdr.cmdtype == SMU_CMDTYPE_WANTS_EVENTS) {
9311071
pp->mode = smu_file_events;
9321072
return 0;
1073+
} else if (hdr.cmdtype == SMU_CMDTYPE_GET_PARTITION) {
1074+
struct smu_sdbp_header *part;
1075+
part = __smu_get_sdb_partition(hdr.cmd, NULL, 1);
1076+
if (part == NULL)
1077+
return -EINVAL;
1078+
else if (IS_ERR(part))
1079+
return PTR_ERR(part);
1080+
return 0;
9331081
} else if (hdr.cmdtype != SMU_CMDTYPE_SMU)
9341082
return -EINVAL;
9351083
else if (pp->mode != smu_file_commands)

0 commit comments

Comments
 (0)